【问题标题】:Share image with ShareActionProvider from Picasso与 Picasso 的 ShareActionProvider 共享图像
【发布时间】:2015-06-20 17:40:18
【问题描述】:

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

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

第一种方式,如下所示,从视图中获取位图并将其加载到文件中。

// 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" />

【问题讨论】:

  • Stack Overflow 用于编写问题。这不是一个问题。如果您想回答自己的问题,那很好,但请提出问题site documentation 有更多关于回答您自己的问题。
  • 对不起..我几个小时前有这个问题,我在stackowerflow上找不到答案,所以决定修复它
  • 我的朋友正在使用它,但它共享了错误的图像 ID,你有这个问题吗?它使用了一个 firebaserecyclerview 和 Picasso
  • 节省我的时间。非常感谢

标签: android image share picasso shareactionprovider


【解决方案1】:

共享图像的第二种方式不需要您将图像写入文件。此代码可以安全地在 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 中获取位图。将该位图放入媒体图像存储中。这为您提供了一个可以用来代替文件路径或 URL 的路径。请注意,原始网页在不可变位图方面存在额外问题,通过将位图绘制到画布中(从未显示在屏幕上)来解决。有关详细信息,请参阅上面的链接页面。

【讨论】:

  • 我无法使用此方法共享任何图像。我每次都会收到提示“没有应用程序可以执行此操作”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多