我不禁注意到,所有使用Intent共享圖像的示例都使用本地存儲的文件。 每當我嘗試使用外部網址時,臉譜,微博等都會給我敬酒說:「一個或多個媒體項目可能無法添加」。Android Intent使用外部圖像url分享
我必須在本地存儲圖像的副本嗎?如果是的話,我該怎麼做?
我不禁注意到,所有使用Intent共享圖像的示例都使用本地存儲的文件。 每當我嘗試使用外部網址時,臉譜,微博等都會給我敬酒說:「一個或多個媒體項目可能無法添加」。Android Intent使用外部圖像url分享
我必須在本地存儲圖像的副本嗎?如果是的話,我該怎麼做?
這裏是你如何可以共享一個鏈接:
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消息不再顯示,但圖像不會添加。你要分享哪個應用程序? – user2799221 2014-10-10 14:23:26
是的圖像無法正常工作 – Pavlos 2017-09-15 11:09:17
有可能的解決方案: Sharing Content with Intents
謝謝@ 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" />
真棒解答,爲我工作 – 2016-02-17 11:51:47
如果我不想顯示它到任何ImageView呢? – santafebound 2016-03-07 00:23:34
我在recyclerview中使用它,它引用了上一張圖像。有小費嗎? – 2017-03-16 18:29:59
另一種替代方式,使用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;
}
[從URL機器人共享圖像(的可能的複製https://stackoverflow.com/questions/16300959/android-share -image-from-url) – 2017-12-27 08:01:26