Android 共享图像时出错
我想使用ACTION_SEND共享图像。所以基本上,当用户点击一个图像并选择“共享图像”时,它应该发送所选的图像, 因此,在测试时,它会告诉我要使用哪个应用程序与whatsapp、Facebook、电子邮件等共享。然后,当选择其中一个应用程序时,它会说“共享失败,请再试一次”。我似乎不明白为什么它不起作用。但是,我有相同的代码,可以用ACTION_视图全屏显示图像文件,这似乎很有效,但不适用于共享Android 共享图像时出错,android,android-intent,android-image,Android,Android Intent,Android Image,我想使用ACTION_SEND共享图像。所以基本上,当用户点击一个图像并选择“共享图像”时,它应该发送所选的图像, 因此,在测试时,它会告诉我要使用哪个应用程序与whatsapp、Facebook、电子邮件等共享。然后,当选择其中一个应用程序时,它会说“共享失败,请再试一次”。我似乎不明白为什么它不起作用。但是,我有相同的代码,可以用ACTION_视图全屏显示图像文件,这似乎很有效,但不适用于共享 public void Onmulti2 (View view) { Intent sh
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!"));
}
步骤1:首先添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
步骤1:首先添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
设置类型image/jpg而不是image/*
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!"));
}设置类型image/jpg而不是image/*
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!"));
}这对我很有效
首先将我的图像存储在外部存储器中
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);
}
});
}
}
####
然后从外部存储器加载我的图像,然后共享它
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"));
}
笔记
从emulator获取路径
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
这对我有用
首先将我的图像存储在外部存储器中
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);
}
});
}
}
####
然后从外部存储器加载我的图像,然后共享它
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"));
}
笔记
从emulator获取路径
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
试试看,字符串imagePath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/ahmed.jpg”;您的映像是否在根目录中?仍然是相同的错误。。。drawable Folder中的我的图像比您以错误的方式拍摄imagepath更适用于从电话目录而不是drawblesTry it中拍摄图像,字符串imagepath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/ahmed.jpg”;您的映像是否在根目录中?仍然是相同的错误。。。drawable Folder中的我的图像比您以错误的方式拍摄图像路径此方法适用于从电话目录而不是Drawble中拍摄图像