Reputation: 748
We have an app which downloads images and store them on the device. The image is stored, but we are not able to see our app folder when we open the gallery. Here is the code we use to create directory:
public File getDataFolder(Context context) {
File dataDir = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
dataDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "MYAPPNAME");
if(!dataDir.isDirectory()) {
dataDir.mkdirs();
}
}
if(!dataDir.isDirectory()) {
dataDir = context.getFilesDir();
}
return dataDir;
}
Also we have noticed, Folder is visible in Jelly bean but not in KitKat+ devices. Why it's not visible in gallery? (files are .jpg format)
Upvotes: 2
Views: 1937
Reputation: 350
Have you tried using getExternalFilesDirs()?
Use getExternalFilesDirs() (note the plural). If that returns more than one entry, the second and subsequent ones are on removable media. Those directories you can read and write to without any permissions on Android 4.4.
From this answer https://stackoverflow.com/a/26006099/5837758.
If the folder appears after rebooting, then you need to add your image to the gallery. From the Taking Photos Simply tutorial (http://developer.android.com/training/camera/photobasics.html):
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
Upvotes: 1
Reputation: 11214
You have to invoke the media scanner on your new file upon that file being visible in the Gallery without reboot. Call the media scaner after ever file saved.
Only a few lines of code will do. Code has been published very often on this site.
Upvotes: 1
Reputation: 739
Prior to Android Kitkat, third-party apps had access to external storage, but from Kitkat, this permission is revoked. Now you can store data in the internal memory of the device only.
Upvotes: 0