我想使用这段代码分享一个图像:
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);
我做了一个按钮,调用上面的代码.分享意向打开,但如果我点击“通过彩信分享”,我得到了“无法将此图片添加到您的消息”,如果Facebook我没有我的照片只有一个文本区域.
2天尝试但没有结果:(
请帮忙
解决方法
我使用这些代码从
this tutorial
final ImageView imgview= (ImageView)findViewById(R.id.FeedImage1); Uri bmpUri = getLocalBitmapUri(imgview); 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 }
然后将其添加到您的活动中
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" />