我编写了以某种方式处理图像的应用程序而不是我想要分享它。允许用户使用标准共享活动共享它的含义。当我这样做时,我收到错误消息。当我选择Gmail时,它会写Can't open empty file
。但文件已保存,我可以使用图库或任何其他应用程序打开它。所以我无法弄清楚我做错了什么。
这是我的分享代码:
public static void shareImage(Bitmap bmp, Context context) {
String pathBitmap = saveBitmap(bmp, context);
if (pathBitmap == null) {
Toast.makeText(context, context.getResources().getString(R.string.save_photo_failed), Toast.LENGTH_SHORT).show();
return;
}
Uri bitmapUri = Uri.parse(pathBitmap);
if (bitmapUri != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
context.startActivity(Intent.createChooser(shareIntent, "Share"));
}
}
以下是我保存位图的方法:
public static String saveBitmap(Bitmap bmp, Context context) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS");
String fileName = sdf.format(new Date(System.currentTimeMillis()));
File fNew = new File(MyApp.getInstance().getPhotosPath(), fileName + ".png");
FileOutputStream out = null;
try {
out = new FileOutputStream(fNew);
bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
out.close();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
Toast.makeText(context, context.getResources().getString(R.string.photos_saved), Toast.LENGTH_SHORT).show();
return fNew.getAbsolutePath();
}
更新
以下是返回路径的函数。从应用createFolder()
onCreate()
private void createFolder() {
_pathPhotos = "";
try {
File folder = new File(Environment.getExternalStorageDirectory(), "WonderApp");
folder.mkdir();
_pathPhotos = folder.getAbsolutePath();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getPhotosPath() {
return _pathPhotos;
}
代码出了什么问题?
答案 0 :(得分:0)
最后我解决了这个问题。实际上问题是here。正如GuiFGDeo所写,问题是URI应该是内容URI而不是文件URI。 Here您可以找到Google建议如何解决问题。我找到了一个更短的方式。
当您将图像插入媒体商店时,您会得到您所需要的内容 - 内容URI。瞧!
public static void shareImage(Bitmap bmp, Context context) {
String sImageUrl = MediaStore.Images.Media.insertImage(context.getContentResolver(), bmp, "title" , "description");
Uri savedImageURI = Uri.parse(sImageUrl);
if (savedImageURI != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, savedImageURI);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "Share"));
}
}