10

I want to create a new folder inside internal storage. But in the new version of Android, we can't create a folder as we created before. But some apps like WhatsApp create folders inside /Android/media/. I want to know about the way how we can create folders inside this location

Dulaj Madusanka
  • 308
  • 2
  • 17
  • yep. But I couldn't find solution there. That's why I post this question. Do you have any solution? – Dulaj Madusanka Oct 21 '21 at 08:31
  • 1
    you have to use [MediaStore API](https://developer.android.com/reference/android/provider/MediaStore) for placing files in `media` folder – snachmsm Oct 21 '21 at 08:49
  • 5
    Your app itself cannot create folders in ../Android/media. But a folder with the packagename of your app will be created if your app calls Context:getExternalMediaDirs(). After that your app can create folders and files in that directory. It is a pitty that some moderating person which hides behind @Commynity closed your question. Maybe it was a duplicate but the provided link is not for your question. – blackapps Oct 21 '21 at 11:23
  • @blackapps thanks that made the trick. upvoted, should be more visible and accepted answer. – Nicola Beghin Oct 24 '21 at 18:48
  • @blackapps can you please explain and post an answer with example code? – Dulaj Madusanka Oct 25 '21 at 06:12
  • Your post is closed so i cannot post an answer. Edit your question and hit the moderators. – blackapps Oct 25 '21 at 06:32
  • Voting for reopen so answer can be posted :) – h4z3 Oct 26 '21 at 08:31
  • @blackapps It really helps, appreciate your answer. Actually nobody provided proper solution of this question. Now my question is, on android 11 and above it needs write external storage permission, so which is the right permission to write to android/media/package folder ? As stated by official doc that WES permission has no effect on Android 11 and above but without this permission we can not write any image or video. Can you please give your feedback on the same. – Smeet Mar 03 '22 at 04:28
  • @blackapps Sorry for misunderstanding, it is working without any permission on Android 11 and above. Not tested below 11. Thank you :) – Smeet Mar 03 '22 at 04:39
  • @smeet can you please share source code here. it will be easy to understand. – Rizwan Rasheed Apr 28 '22 at 11:15

1 Answers1

3

context.getExternalMediaDirs() will return you all the folders from media folder and when you call that it will create a folder with your app's package name all you have to do is identify packagename and get your folder path. This function will return you full path to your folder inside media folder. But it will not work for lollipop and less versions.

 public static String create_folder_in_app_package_media_dir_new2(Context context) {

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     File[] directory = new File[0];

     directory = context.getExternalMediaDirs();


 for(int i = 0;i<directory.length;i++){

         if(directory[i].getName().contains(context.getPackageName())){
             return directory[i].getAbsolutePath();
         }

     }

 }

     return null;
}

Note : If you delete or uninstall your application from device this folder will also be removed and data inside this folder will also be removed.