android - 使用 Picasso 的 ShareActionProvider 分享图像

标签 android image share picasso shareactionprovider

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

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

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

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

最佳答案

共享图像的第二种方式不需要您将图像写入文件。此代码可以在 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 的路径。请注意,原始网页还有一个与不可变位图有关的额外问题,通过将位图绘制到 Canvas (从未在屏幕上显示)来解决。有关详细信息,请参阅上面的链接页面。

关于android - 使用 Picasso 的 ShareActionProvider 分享图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29638747/

相关文章:

android - 我是否需要我的移动应用程序中的私钥才能通过 SSL 与服务器通信?

asp.net - Internet Explorer : whitelines around pictures, 缺少图片

javascript - 如何在单击时切换两个图像

facebook - 在 Facebook 帖子上的“赞”和“评论”旁边添加“分享”按钮

Javascript Windows 应用商店应用程序共享魅力

android - 主题不适用于以编程方式添加的 View ?

java - 如何为像谷歌信使这样的聊天应用程序制作时间戳

java - Realm 配置对象类型 java

ios - 点击图像时如何禁用 phonegap/cordova 上的图像缩放?

java - Twitter 分享带有文本的图像,toast "Image Could Not Be Loaded"