0

In one part of my app i need to save captured pictures by user and place them in a folder i want to create so i can query that folder later and pick the last picture in it and show it to user as an overview. i know that i can save pictures in public shared pictures folder like this:

 ContentValues contentValues = new ContentValues();
 contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, 
 Environment.DIRECTORY_PICTURES);
 contentValues.put(MediaStore.Images.Media.IS_PENDING, true);
     

 ContentResolver contentResolver = context.getContentResolver();

 Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
 contentValues);

 if (uri != null) {

 //convert the pic as byte array to bitmap
     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

     try {

         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, contentResolver.openOutputStream(uri));
         contentValues.put(MediaStore.Images.Media.IS_PENDING, false);
         contentResolver.update(uri, contentValues, null, null);

     } catch (Exception e) {

                   
     }
 } 

But the problem is if there is any other picture in the shared folder my app would pick the last one and show it regardless of it being captured by my app or other apps

My question is do we have any way to create folder in android? (other than data folder or use MANAGE_EXTERNAL_STORAGE permission) or we just have to use public shared folderd without any inner folder because i've tried MediaStore and File Api and i can't do it.

erfan
  • 81
  • 1
  • 11
  • Use `ACTION_OPEN_DOCUMENT_TREE` to allow the user to choose a document tree ("folder"). You can create a sub-tree ("folder") inside of there and put content in there as you see fit. Plus, the user gets to control where on the user's device (or the user's cloud storage) you put the user's content. – CommonsWare Mar 25 '21 at 22:53
  • 1
    Use for RELATIVE_PATH DCIM/MyFolder or Pictures/MyFolder. – blackapps Mar 25 '21 at 23:00
  • 1
    Further: On an Android 11 device you can just create your image files in DCIM/MyFolder without using MediaStore. Just use classic file io. – blackapps Mar 25 '21 at 23:02
  • 1
    @blackapps Thank you looks like i was making a silly mistake and i forgot about adding "/" before my folder name so i edited my code like this `contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/My folder");` and now i can create folder in Downloads Pictures and DCIM folders and save my pictures even in android 11. – erfan Mar 26 '21 at 14:07

0 Answers0