使用Picasso从ShareActionProvider分享图片

9

我花了几个小时才找到这个解决方案……

所以我决定分享这个信息,也许对某些人会有帮助 :)

下面展示的是第一种方法,它将从视图中获取位图并将其加载到文件中。

// Get access to ImageView 
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Fire async request to load image
Picasso.with(context).load(imageUrl).into(ivImage);

然后,在图像加载完成后,您可以触发共享操作:

// Can be triggered by a view event such as a button press
public void onShareItem(View v) {
    // Get access to bitmap image from view
    ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
    // Get access to the URI for the bitmap
    Uri bmpUri = getLocalBitmapUri(ivImage);
    if (bmpUri != null) {
        // Construct a ShareIntent with link to image
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.setType("image/*");
        // Launch sharing dialog for image
        startActivity(Intent.createChooser(shareIntent, "Share Image"));    
    } else {
        // ...sharing failed, handle error
    }
}

// Returns the URI path to the Bitmap displayed in specified ImageView
public Uri getLocalBitmapUri(ImageView imageView) {
    // Extract Bitmap from ImageView drawable
    Drawable drawable = imageView.getDrawable();
    Bitmap bmp = null;
    if (drawable instanceof BitmapDrawable){
       bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
    } else {
       return null;
    }
    // Store image to default external storage directory
    Uri bmpUri = null;
    try {
        File file =  new File(Environment.getExternalStoragePublicDirectory(  
            Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
        file.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.close();
        bmpUri = Uri.fromFile(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmpUri;
}

请确保在AndroidManifest.xml文件中添加适当的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

1
Stack Overflow 是用于编程问题的。这不是一个问题。如果您想回答自己的问题,那很好,但请提出一个问题。网站文档有更多关于回答自己问题的信息。 - CommonsWare
对不起,我几个小时前提出了这个问题,但在StackOverflow上没有找到答案,因此决定自己解决。 - Stan Malcolm
我的朋友正在使用这个,但它共享了错误的图像ID,你遇到过这个问题吗?它正在使用firebaserecyclerview和Picasso。 - A P
节省我的时间。非常感谢。 - Priyanka
1个回答

7

第二种方式分享图片不需要将图片写入文件中。此代码可以在UI线程上安全地执行。这个方法是在这个网页上建议的 http://www.nurne.com/2012/07/android-how-to-attach-image-file-from.html

ImageView siv = (ImageView) findViewById(R.id.ivResult);
Drawable mDrawable = siv.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();

String path = Images.Media.insertImage(getContentResolver(), 
    mBitmap, "Image Description", null);

Uri uri = Uri.parse(path);
return uri;

从ImageView获取Drawable,从Drawable获取Bitmap,将该位图放入媒体图像存储中。这会给你一个可以用来替代文件路径或URL的路径。请注意,原始网页存在一个不可变位图的额外问题,通过将位图绘制到画布中(从未显示在屏幕上)解决。有关详细信息,请参见上面链接的页面。


我无法使用这种方法分享任何图像。每次都会弹出“没有应用程序可以执行此操作”的提示。 - Martin Erlic
即使在此时,这仍然有效。我喜欢这个解决方案,因为我不必担心必须将图像保存到文件系统中,因为这是不必要的。我想分享一个位图快照,因此不需要保存。 - chitgoks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接