Reputation: 1174
I write the app that process image somehow and than I want to share it. Meaning to allow user to share it using standard share activity. When I do this I get error messages. When I choose Gmail it writes Can't open empty file
. But the file is saved, I can open it with the Gallery or any other app. So I can not figure out what I do wrong.
Here is my sharing code:
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"));
}
}
Here is how I save the bitmap:
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();
}
UPDATE
Here are functions that returns the path. createFolder()
is called from apps 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;
}
What's wrong with the code?
Upvotes: 1
Views: 1500
Reputation: 1174
Finally I resolved the problem. The problem actually was described here. As GuiFGDeo kindly wrote, the problem is URI that should be content URI instead of file URI. Here you can find how Google propose to solve the problem. I found a bit shorter way.
When you insert image into media store you receive exactly what you need - content URI. And voilà!
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"));
}
}
Upvotes: 1