android - delete special file name with ( character - android

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.

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

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.

Android exception 'open failed: EACCES (Permission denied)' - not due to the SD card

I am having a problem copying files (.so binaries) from my assets directory to the app installation "/data/data/name.ofmy.app/lib" directory.
When I try to I get the following error:
07-09 11:00:48.902: W/System.err(25122): java.io.FileNotFoundException: /data/data/name.ofmy.app/lib/libOlsrdPudWireFormat.so: open failed: EACCES (Permission denied)
This happens when the following code is being ececuted:
if ((so = new File(dir, fileName)).exists()) {
/*
* Check to see if this timestamp pre-dates the current package
*/
if ((pkg = new File(ctx.getPackageResourcePath())).exists()) {
if (pkg.lastModified() < so.lastModified()) {
Log.v(TAG, "extractFile: File up to date");
return;
}
}
Log.v(TAG, "extractFile: library present but out of date");
so.delete();
}
Log.v(TAG, "extractFile: copying library");
InputStream in = ctx.getAssets().open(name);
FileOutputStream out = new FileOutputStream(so);
It breaks right at InputStream in = ctx.getAssets().open(name); when there is not an already existing file with that name...
I have been reading similar questions here on Stack Overflow, but unfortunately that did not help. Most are copying to the SD card, but I am not.
Also the strange part is that a previous directory (/bin instead of /lib), which is also supplied within my assets, does get copied without any problem in the same /dat/data... directory!
I tried adding the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission to no avail.
I even tried adding Runtime.getRuntime().exec("su"); just before I try to copy anything, also to no avail.
It's a rooted phone BTW!
I also double checked if the file is really there in the assets, and it is!
Just for the sake of trying I changed the directory name from lib to libs (because as mentioned in my question, I also copied another directory named /bin without any issues) and guess what: now it works!
It seems you cannot use the /lib directoryname whilst copying. I have no idea why, though.
Definitely you forgot to put in the manifest:
<manifest
.
.
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

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