我想使用ACTION_SEND分享图片。所以基本上当用户点击图像并选择“共享图像”时,它应该发送所选的图像, 因此,在测试时,我需要使用哪个应用程序与whatsapp,Facebook,电子邮件等共享。然后在选择其中任何一个时,它会显示“共享失败,请再试一次”。我似乎无法弄清楚为什么它不起作用。但是我有相同的代码用ACTION_VIEW全屏显示图像文件,这似乎很好但不是共享。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
答案 0 :(得分:1)
设置类型图像/ jpg而不是图像/ *
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpg");
String imagePath = Environment.getExternalStorageDirectory()
+ "/ahmed.jpg";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
答案 1 :(得分:1)
这对我有用
首先将我的图像存储在外部存储空间
private void SaveImage(Bitmap finalBitmap) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
String fname = "image.jpg";
File file = new File(myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.e("ExternalStorage", "Scanned " + path + ":");
Log.e("ExternalStorage", "-> uri=" + uri);
}
});
}
}
##。 ##
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ahmed);
SaveImage(img);
}
然后从外部存储加载我的图像,然后共享它。
public void Onmulti2 (View view) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/saved_images/image.jpg"));
startActivity(Intent.createChooser(share, "Share image using"));
}
从模拟器获取路径
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: Scanned /storage/emulated/0/saved_images/image.jpg:
01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: -> uri=content://media/external/images/media/106290
答案 2 :(得分:0)
第1步:首先添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第2步:修改
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath);