11

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

10
  • yep. But I couldn't find solution there. That's why I post this question. Do you have any solution? Commented Oct 21, 2021 at 8:31
  • 1
    you have to use MediaStore API for placing files in media folder
    – snachmsm
    Commented Oct 21, 2021 at 8: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
    Commented Oct 21, 2021 at 11:23
  • @blackapps thanks that made the trick. upvoted, should be more visible and accepted answer. Commented Oct 24, 2021 at 18:48
  • @blackapps can you please explain and post an answer with example code? Commented Oct 25, 2021 at 6:12

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.