2014-10-10 91 views
6

我不禁注意到,所有使用Intent共享图像的示例都使用本地存储的文件。 每当我尝试使用外部网址时,脸谱,微博等都会给我敬酒说:“一个或多个媒体项目可能无法添加”。Android Intent使用外部图像url分享

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

+0

[从URL机器人共享图像(的可能的复制https://stackoverflow.com/questions/16300959/android-share -image-from-url) – 2017-12-27 08:01:26

回答

4

这里是你如何可以共享一个链接:

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); 
+3

Toast消息不再显示,但图像不会添加。你要分享哪个应用程序? – user2799221 2014-10-10 14:23:26

+0

是的图像无法正常工作 – Pavlos 2017-09-15 11:09:17

17

谢谢@ 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); 

成功加载到图像视图图像后,触发共享意图的方法。

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

真棒解答,为我工作 – 2016-02-17 11:51:47

+3

如果我不想显示它到任何ImageView呢? – santafebound 2016-03-07 00:23:34

+0

我在recyclerview中使用它,它引用了上一张图像。有小费吗? – 2017-03-16 18:29:59

-1

另一种替代方式,使用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; 
}