I am creating a folder through my app
File direct_playlist = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + PLAYLIST_PATH);
if(!direct_playlist.exists())
{
if(direct_playlist.mkdir()); //directory is created;
}
Even though the command executes perfectly I can not see the folder in Explorer. I have also given permission to my app in the manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
When I try to access this folder my app crashes as the folder is not present.
This topic is already discussed So Use Following threads..
How to check if newly created folder is present into SD Card in Android
Creating a directory in /sdcard fails
It will surely help you.
Related
Asking this questions after trying a lot. I have checked a lot ob blogs and questions.
When I'm trying to create a new folder in the device's doucments folder, its not creating the folder even after giving all permissions including runtime permissions.
This is my code to create the new folder -
var path = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS))
var directory = path.absolutePath + "/justCheck"
var file = File(directory)
if(!file.exists()){
file.createNewFile()
}
I'm checking on Android device with marshmallow OS. This is my permissions in Manifest -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also, I have authorized the runtime permissions.
Am I doing anything wrong ? Why is the older not getting created ?
You've to create a folder not a file:
file.mkdirs();
I'm trying to use expansion files and I found an issue that I'm not able to resolve.
It seems that you can access the /Android/obb folder and eventually delete the /Android/obb/my.package.name directory (tried with some file manager), but I cannot handle this situation whithin the app.
If I just let the Google Downloader library try to download the expansion file it will hang and error (for the missing folder), but it will start if I recreate the folder.
The strange thing is that I'm able to create the folder from other apps (the same file manager that I've used to see the directory) but not from mine!
I tried with
File f = new File(Environment.getExternalStorageDirectory(), "Android/obb/my.package.name");
f.mkdirs();
but it doesn't work.
I've the permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and I'm targeting api 23 and compiling with the 25.
OP dropped the Target SDK to 22 and was able to avoid Dangerous permission work flow which was causing the issue.
I had this same problem, but lowering the Target SDK was not an option.
The Context::getObbDir() method allows access to the expansion folder without the usual security rules. I found that, if the folder doesn't exist, getObbDir() creates it too (You can also double check and create it manually with mkdir()).
Excerpt from the documentation linked above:
Return the primary shared/external storage directory where this application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, this directory may not exist.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:
... Starting in Build.VERSION_CODES.KITKAT, no permissions are required to read or write to the path that this method returns. ...
Starting from Build.VERSION_CODES.N, Manifest.permission.READ_EXTERNAL_STORAGE permission is not required, so don’t ask for this permission at runtime. ...
So you can do something like:
File obbDir = getObbDir();
if (null == obbDir) {
// Storage is not available
} else if (!obbDir.exists() && !obbDir.mkdir()) {
// Failed to create directory. Shouldn't happen but you never know.
}
NOTE: You may need the read/write permissions to access the expansion files within the folder.
Currently
I'm storing my file(images/videos) like this:
File directoryToStore;
directoryToStore = getBaseContext().getExternalFilesDir("MyImages");
This will return this path:
/storage/emulated/0/Android/data/pacageName/files/MyImages/
Now, I want to store the files in root directory /storage/emulated/0/MyImages/. I have tried this by doing:
File directoryToStore;
directoryToStore = new File(Environment.getExternalStorageDirectory(), "MyImages");
This works perfectly fine when running on pre-Marshmallow devices, but In Marshmallow the files are not found.
My Question
How should/can I store files in the root directory so that the file will be found in all API's?
Firstly, make sure you have the permissions bellow in your Android Manifest file, because you are trying to save files into the device's external storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Secondly, check if storage permissions are granted to your application (when you execute on Marshmallow or higher OS version devices).
Finally, make sure if there is directory named "MyImages" and check it's chmod. If directory does not exist, create it (mkdir() will create the directory in your example) and then try to save your files again.
First of all, I'm an android newbie. Now my question...
I want to create a folder with some files, so I can easily access them when I plug in my droid via USB.
"directory.exists" returns true, saying the folder exists, but when I go to Windows File Explorer, Computer\Nexus 5\Internal storage, I do not see my app folder listed in there.
getExternalStorageDirectory returns: /storage/emulated/0/MyAppName
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppName/");
if (!directory.exists()) {
directory.mkdir();
}
I have added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in manifest.
What am I doing wrong?
You're not doing anything wrong. Android does.
That MTP connection is unreliable as contents displayed is not in sync with the latest files wrote on the phone.
You can cause a manual rescan or reboot the phone... see below:
android bug
similar question with workarounds
How can I delete a file from an Android application? Can I do it the same way I would for deleting a file in Java?
File file = new File(selectedFilePath);
boolean deleted = file.delete();
and set this permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest.xml file
The Android Developer Homepage has a really good Dev Guide section.
Answer to your question:
Files can be deleted using: File.delete() method. (I found that by searching for "file delete" on the page above!)
But of course, there's much more to that: you need to understand how Android stores files and which files your application is allowed to modify! (basically only its own files, all the others are not accessible)
File file = new File(path);
return file.delete();