How to delete a file from an Android application - android

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();

Related

Android assets no such file or directory

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.

Making Directory In Android Studio

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

android - delete special file name with ( character

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.

How to create folder in Computer\Nexus 5\Internal storage folder

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 to delete files in Android?

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.

Categories

Resources