Sta*_*olm 8 android share image shareactionprovider picasso
我花了几个小时才找到这个解决方案......
所以我决定分享这个信息,也许有一个它会有用:)
第一种方法(如下所示)从视图中获取位图并将其加载到文件中.
// Get access to ImageView
ImageView ivImage = (ImageView) findViewById(R.id.ivResult);
// Fire async request to load image
Picasso.with(context).load(imageUrl).into(ivImage);
Run Code Online (Sandbox Code Playgroud)
然后假设在图像完成加载后,这就是你可以触发共享的方式:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
确保为AndroidManifest.xml添加适当的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Run Code Online (Sandbox Code Playgroud)
共享图像的第二种方法不需要您将图像写入文件。可以在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;
Run Code Online (Sandbox Code Playgroud)
您从ImageView获得Drawable。您从Drawable获得位图。将该位图放入媒体图像存储中。这为您提供了一个可以代替文件路径或URL的路径。请注意,原始网页还有一个不变的位图的问题,可以通过将位图绘制到画布中来解决(屏幕上从未显示)。有关详细信息,请参见上面的链接页面。
归档时间: |
|
查看次数: |
3163 次 |
最近记录: |