Reputation: 674
I'm working on a meme generator. Right now, I'm trying upload a photo from my device, to put captions on the photo through Canvas
and save the new bitmap to my device's "Gallery" app by creating a new folder in it. This is my attempt:
public void createBitmapAndSave(ImageView img){
BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
String topText = topTextView.getText().toString();
String bottomText = bottomTextView.getText().toString();
canvas.drawText(topText, 0, 0, paint);
canvas.drawText(bottomText, 50, 50, paint);
File file;
String path = Environment.getExternalStorageDirectory().toString();
file = new File(path, "Meme" + ".jpg");
try{
OutputStream stream = null;
stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
}catch (IOException e){ e.printStackTrace(); }
}
And I'm calling the method in my BroadcastReceiver
:
private BroadcastReceiver listener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent ) {
String data = intent.getStringExtra("DATA");
Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();
createBitmapAndSave(imageView);
}
};
At the moment, I'm not even sure if the editing or the saving of the bitmap even worked because I couldn't find it anywhere in my app's folders.
UPDATE
I've found the issue. I decided to place a toast message inside the try/catch block to see if the stream is working at all because before it was outside the try/catch block and I'd see it at each run. This is what I did:
public void createBitmapAndSave(ImageView img){
BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());
Bitmap bitmap = bitmapDrawable.getBitmap();
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
String topText = topTextView.getText().toString();
String bottomText = bottomTextView.getText().toString();
canvas.drawText(topText, 0, 0, paint);
canvas.drawText(bottomText, 50, 50, paint);
File file;
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
file = new File(path, "Meme" + ".jpg");
try{
OutputStream stream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream);
stream.flush();
stream.close();
Toast.makeText(getContext(), path, Toast.LENGTH_SHORT).show();
}catch (IOException e){ e.printStackTrace();}
}
Now, I don't see the toast message at all. It seems like the stream is not even working properly but I don't see why it wouldn't.
Upvotes: 0
Views: 53
Reputation: 1118
Don't use String path = Environment.getExternalStorageDirectory().toString();
it will store your file in private storage that is not visible to user.
use File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
Upvotes: 1