Store images using MediaStore in a specific folder

I was looking a bit about how to add images using the mediator, as I am doing this manually now with

final String myBitmap = "MyImage_"+ mFileDate + ".png";
File fileNumber = new File(APP_FILE_PATH, myBitmap);
out = new FileOutputStream( fileNumber );
final Bitmap mBitmap =getBitmap();
mBitmap.compress( Bitmap.CompressFormat.PNG, 85, out );
out.flush();

however, I need the identifiers that the media manager uses for storage, as it simplifies the management of the identifier, not the paths, etc. I tried adding it like this:

ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, mFileName.getName());
values.put(Images.Media.DESCRIPTION, getString(R.string.bitdraw_description));
values.put(Images.Media.MIME_TYPE, "image/png");

Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
out = getContentResolver().openOutputStream(uri);
mBitmap.compress( Bitmap.CompressFormat.PNG, 85, out );
out.flush();

however, this adds the image to the folder where the photos are saved, and not to the desired folder in which I want.

Also tried this after the tutorial (first part):

Media.insertImage(getContentResolver(), mFileName.getPath(), mFileName.getName(), getString(R.string.description));

but this only creates duplicates, since the image was first saved using outputStream

The workaround I found in this thread is to trigger the broadcast as follows:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse ("file://"+ Environment.getExternalStorageDirectory())));

, , , , , , SD?? .

+3

All Articles