如何为 ImageView 创建共享意图?

发布于 2024-12-17 19:01:48 字数 1139 浏览 4 评论 0原文

我正在创建一个照片应用程序,用户从图库中选择图像,并将其显示在应用程序中心的图像视图中。我将如何为 ImageView 创建共享意图?

更新 共享意图正在工作,但是,图像无法共享,因为我无法将其保存到我选择的路径。下面是我的代码。有什么帮助吗?

public void taptoshare(View v)
    {  
        View content = findViewById(R.id.myimage);
        content.setDrawingCacheEnabled(true);
            Bitmap bitmap = content.getDrawingCache();
            File file = new File("/DCIM/Camera/image.jpg");
            try 
            {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.JPEG, 100, ostream);
                ostream.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }


        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
        shareIntent.setData(phototUri);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
        startActivity(Intent.createChooser(shareIntent, "Share Via"));

}  

}

I am creating a photo application where the user selects and image from the gallery and it displays in an imageview in the center of the app. How would I go about creating a share intent for the ImageView?

UPDATE
Share intent is working, however, the image can not be shared because I can not save it to the path I have selected. Below is my code. Any help?

public void taptoshare(View v)
    {  
        View content = findViewById(R.id.myimage);
        content.setDrawingCacheEnabled(true);
            Bitmap bitmap = content.getDrawingCache();
            File file = new File("/DCIM/Camera/image.jpg");
            try 
            {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.JPEG, 100, ostream);
                ostream.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }


        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
        shareIntent.setData(phototUri);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
        startActivity(Intent.createChooser(shareIntent, "Share Via"));

}  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩世 2024-12-24 19:01:48

试试这个

Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(path);
shareIntent.setData(photootUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, photootUri);
getContext().startActivity(Intent.createChooser(shareIntent, "Use this for sharing"));

Try this

Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(path);
shareIntent.setData(photootUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, photootUri);
getContext().startActivity(Intent.createChooser(shareIntent, "Use this for sharing"));
轻许诺言 2024-12-24 19:01:48

请参阅下面的链接。这对你很有帮助。我认为它满足了你的要求。

http://www.technotalkative.com/android-pick-image-来自本地画廊/

See the below link. it will very helpful for u. I think it satisfied your requirement.

http://www.technotalkative.com/android-pick-image-from-native-gallery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文