I am having trouble with creating a directory with android studio on my app. The problem is that my code is simply not creating a directory, but I can't seem to tell what the problem is. Here's my code:
File folder = new File(Environment.getExternalStorageDirectory() + "/folder");
folder.mkdir();
I have also edited my manifests file to add this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Also, I have no duplicates of the folder either.
Help would be great! Thanks!
Try this code
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();
Reference here
Related
In my android project I have created an assets folder in which I have added some json files and I want to read them with a stringBuffer. The assets folder is listed inside src/main. So far I have added the following code:
String[] files = assetManager.list("");
ArrayList<String> it = new ArrayList<>(Arrays.asList(files));
And so far I am getting the names of the files successfully inside my ArrayList. Then I am trying this:
InputStream input = assetManager.open("filename.json");
But I get the following error:
Failed to open file
'/data/data/package/code_cache/.overlay/base.apk/assets/filename.json':
No such file or directory
I also have included the follwing permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Any idea what might be the problem here?
You can clear storage in device and then rebuild app, that help my same issue.
I got the same issue on my Android project many times. Try uninstalling the app on your emulator and rebuild your app.
I run below code to delete a file in android but doesn't work:
origin file name:
/storage/emulated/0/concreteangelradioedit (1).mp3
I re-encode it to:
/storage/emulated/0/concreteangelradioedit%20%281%29.mp3
Code section:
File file = new File(filename);
file.delete();
Instead of using a URL encoding, try escaping your characters. See what happens with the following
/storage/emulated/0/concreteangelradioedit\ \(1\).mp3
Hah, stackoverflow is taking my escapings into effect, be sure there is a single \ in front of the space, and each of the parenthesis.
Make sure you also have to following permission defined in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
edit
Huh... have you tried simply deleting the file name as is without the encoding?
File f = new File("/storage/emulated/0/concreteangelradioedit (1).mp3");
f.delete();
Not android, but using java on my local machine that worked without a problem.
I have a file with this path:
file:/mnt/sdcard/Android/data/myapp/files/Pictures/IMG_20140108_160223.jpg
When I try this code:
File f = new File(path);
f.delete();
The file is not deleted.
How can I do?
I've found a similar question here:
How to delete a file from SD card?
The file: prefix seems unnecessary.
try/mnt/sdcard/Android/data/myapp/files/Pictures/IMG_20140108_160223.jpg
Also you have to give permission if you are using >1.6 SDK
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in AndroidManifest.xml file
You may need to use
file.getCanonicalFile().delete();
or even (assumming this is a context)
this.deleteFile("string");
More Infos here to delete file with context object
You may also delete your file:/ at the beginning of your File object creation.
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.
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();