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?