【问题标题】:Android Intent Share using external image urlAndroid Intent Share 使用外部图片 url
【发布时间】:2014-12-05 17:26:15
【问题描述】:

我不禁注意到,所有与 Intent 共享图像的示例都使用本地存储的文件。 每当我尝试使用外部网址时,facebook、twitter 等都会给我祝酒词“无法添加一个或多个媒体项目”。

我必须在本地存储图像的副本吗?如果是,我该怎么做?

【问题讨论】:

标签: android android-intent share


【解决方案1】:

感谢@user2245247 提供link
它包含从远程 url 共享图像的正确答案。
使用外部库

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

图片成功加载到image view后,触发分享intent的方法。

// 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;
}

确保您具有以下权限。

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

【讨论】:

  • 如果我不想将它显示给任何 ImageView 怎么办?
  • 我在 recyclerview 中使用它,它引用了以前的图像。有什么建议吗?
  • 如果我使用了另一个库,例如 Fresco,该怎么办?它有 SimpleDraweView 而不是 ImageView
  • 由于某种原因它失败了。你知道为什么吗?
【解决方案2】:

分享链接的方法如下:

Intent intent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.parse("http://linkto.com/your_image.png");
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, String.valueOf(uri));
startActivity(intent);

【讨论】:

  • toast 消息不再显示,但图像不会添加。您要分享到哪个应用?
【解决方案3】:

使用method from previous answer 实现此目的的另一种方法是:

Picasso.with(this).load(imageurl).into(shareTarget);

private Target shareTarget = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        // Get access to the URI for the bitmap
        Uri bmpUri = getLocalBitmapUri(bitmap);
        if (bmpUri != null) {
            // Construct a ShareIntent with link to image
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text));
            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
        }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};

public Uri getLocalBitmapUri(Bitmap bmp) {
    // 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2012-01-22
    • 2018-03-07
    相关资源
    最近更新 更多