我想使用代码分享一张图片:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri imageUri = Uri.parse("http://stacktoheap.com/images/stackoverflow.png");
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(sharingIntent);
我做了一个按钮来调用上面的代码。共享 Intent 打开,但如果我单击“通过彩信共享”,我得到了:“无法将此图片添加到您的消息中”。如果 Facebook 我只有一个没有我的图片的文本区域。
最佳答案
不需要使用 ImageView 的 @eclass 答案的改编版本:
使用 Picasso将 url 加载到位图中
public void shareItem(String url) {
Picasso.with(getApplicationContext()).load(url).into(new Target() {
@Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
startActivity(Intent.createChooser(i, "Share Image"));
}
@Override public void onBitmapFailed(Drawable errorDrawable) { }
@Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
}
将位图转换为 Uri
public Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
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;
}
关于android从url分享图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16300959/