1

I have created a folder in the internal storage for storing files. But the issue is the folder is not created in some Android devices. The folder is created in lollipop devices but not in oreo and nougat.

  • What is the problem?
  • Is there any additional permission is needed for higher level API?

The code for creating a folder in internal storage

File f1 = new File(Environment.getExternalStorageDirectory(), Constants.STORED_FOLDER);
if (!f1.exists()) {
  f1.mkdirs();
}
Log.e("check_path", "" + f1.getAbsolutePath());

Also, I have another query on how to make the folder visible in the gallery.

It is not visible in the gallery? How to solve this issue?

shyam
  • 9,134
  • 4
  • 29
  • 44
Neetha
  • 109
  • 1
  • 2
  • 10

3 Answers3

7

The folder is created in lollipop devices but not in oreo and nougat. What is the problem?

Since Android 6.0 (If my memory right), many permissions needs to be assigned dynamically (at runtime). You need to get your permission at runtime.

Is there any additional permission is needed for higher level API?

The basic permissions are WRITE_EXTERNAL_STORAGE, but you not only needs to declare it in AndroidManifest.xml, but also at runtime.

Also I have another query how to make the folder visible in gallery. It is not visible in gallery? How to solve these issue?

If you mean the photo gallery, you may need to wait for the media process to scan the whole sdcard to get your folder (which must contain some photos) added in gallery.

Geno Chen
  • 4,916
  • 6
  • 21
  • 39
  • A question generated from my answer: Is it possible for app to forcibly add their folder in gallery? – Geno Chen Feb 07 '19 at 05:27
  • The folder that I am created in internal storage is not visible in gallery. So each time I want to visit the File Explorer to check the folder. How it make visible in gallery? – Neetha Feb 07 '19 at 12:04
  • @Neetha See the answer by Dhiraj below. – Geno Chen Feb 08 '19 at 06:27
  • 1
    Thank you. I have done the folder creation as you recommended. And I also make it visible in gallery by using MediaScannerConnection in android – Neetha Feb 08 '19 at 15:28
2

For show up your folder immediately, you need to send a broadcast

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

this code will tell the gallery app that something has been added so please rescan for media now

Dhiraj Choudhary
  • 195
  • 5
  • 16
  • 1
    This answer is add on to Geno Chen Answer – Dhiraj Choudhary Feb 07 '19 at 05:38
  • Can you please tell me where I want to place this code? Also what is "file://" here? Is this our folder name? – Neetha Feb 07 '19 at 11:05
  • @Neetha I think, place it to where after you put photos inside that folder. "file://" is a default schema for file, specified by URI format. You may manually rewrite the parameter inside `Uri.parse()` to your photo dir. – Geno Chen Feb 08 '19 at 06:30
0

you must add request permission before create folder. see links below : https://developer.android.com/training/permissions/requesting

Android marshmallow request permission?

mohamad sheikhi
  • 394
  • 5
  • 16