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

Related

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.

Android SDcard Permissopn denied while writing a file?

In my application, while writing a file sdcard it shows Permission denied. But i give permission in sdcard. Please help?
Writing file code:
File sdcard = Environment.getExternalStorageDirectory();
File f = new File(sdcard, "/abc.txt");
if (f.exists()) {
f.delete();
}
Manifest :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Thanks in advance!!
You need add use-permission in AndroidManifest file, and you should better check if you can r/w the file use canWrite() canRead() before use it.
I think your SD card is currently connect to your computer so it will cause problem. I get in this situation before. You need to unmount sd card first
it requires permissions in manifest file, add this line to your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Look at this Question Android saving file to external storage
in menifest file add permission to write file
pls try with file name as below
String filename="aa.txt";
String filepath= getFilesDir().getAbsolutePath();
File Filseting = new File(filepath, filename);

Read CSV File in android app

Thanks in advance,
How to read csv file from sdcard.
and how to give the permission of read/writer for file in android application.
thanks again.
This might be helpful to you
File dir = Environment.getExternalStorageDirectory();
File f = new File(dir, "path/dir1/sdcard.ext");
I don't know about the permissions but the above will certainly allow you to read the file from the sdcard in the path directory.

How to delete a file from an Android application

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

Categories

Resources