0

Im trying to implement a small file Explore within my app in order to give to the user the ability to select new path (to store X) instead of the default path, also i want the user to be able to create and delete file.

So far my code is working however there is something really weird, im able to delete only file that i created myself within the app.

I have read a lot of file deletion tutorial example, here and there and i last time i check i have both read/write permission in my app manifest but still i cant delete files others than the one i created. here is the latest deletion code:

File file = new File(customAdapter.getItem(position).getFile().getAbsolutePath());
boolean  isDeleted = file.delete();

if(!isDeleted){
    Log.e("File manager: ",customAdapter.getItem(position).getFile().getName()+" deletion Fail");
} else {
    fileHolderList.remove(position);
    customAdapter.notifyDataSetChanged();
}

I have also read somewhere that for some security purposes android allow you to delete only file that your package created, ok but what about those file Explorer app in the play store which allow you a full control of your sd card or internal storage files. How do they did that? So that mean there is a way.

Please anyone can help with that?

Edit: i mean by file here Folder.

indivisible
  • 4,892
  • 4
  • 31
  • 50
murielK
  • 1,000
  • 1
  • 10
  • 21
  • As a side note to your problem; you should use more meaningful variable names. If you had called your File 'directory', 'dirToDelete' or something more descriptive other than 'file' there would likely have been less confusion. – indivisible Mar 01 '14 at 09:47

1 Answers1

3

You can only modify files located on SDCard type, public storage. Whether real or emulated. Are you trying to modify files from the Internal storage? You need root permissions for changing anything outside your app's default data directories on the Internal Storage.

Since I now know that you are attempting to delete directories I assume that you are trying to delete directories that contain files. You should recursively delete the directory's contents before deleting the directory itself. See this question for how to go about that: https://stackoverflow.com/a/4026761/1590950

Community
  • 1
  • 1
indivisible
  • 4,892
  • 4
  • 31
  • 50
  • SDcard storage...both real and emulated – murielK Mar 01 '14 at 09:08
  • Yes. Either a real SDCard that is inserted into the device for extended storage or a software partition of the device's internal storage that acts and appears to Android as an SDCard. – indivisible Mar 01 '14 at 09:10
  • Both real and emulated! of course not at the same time real for device with sd card emulated for device without...! – murielK Mar 01 '14 at 09:13
  • I own a Galaxy S3 and it has both real and emulated SD storage. So I have 3 usable storage partitions: SD real, SD emulated and the Internal storage. I'm not sure which one of us is not understanding the other but I'm a little confused now... Can you tell me the exact location of the files you are trying to delete? The absolute path would be very helpful. – indivisible Mar 01 '14 at 09:21
  • Simple . when you have both real and emulated it will go to emulated one, when u have only emulated it will be automatically save to emulated , when its only real ---> saved to sd. and we are talking here only about SD either is emulate or not. the default path will be your SdRoot/... this is where im trying to delete file (by the way im trying to delete only directories ) – murielK Mar 01 '14 at 09:29
  • If it's directories you are trying to delete I'd imagine the issue is that they contain files. You should recursively delete the directory's contents before deleting the directory itself. Have a look at this question: http://stackoverflow.com/questions/779519/delete-files-recursively-in-java – indivisible Mar 01 '14 at 09:34
  • Ok let me have a look . – murielK Mar 01 '14 at 09:39
  • i dont have this FileUtils in android but i got you are right i will implement an algorithm to delete all file content first before deleting folder. Thank you – murielK Mar 01 '14 at 10:01
  • Using ApacheCommons is a little overkill for this issue. One of the lower answers on the question is a simple recursive method solution in plain java. I linked directly to that answer in my answer above. I can't edit my comment here though. – indivisible Mar 01 '14 at 10:03
  • You are right simple recursive method solve the problem. you made my day :D – murielK Mar 01 '14 at 10:05
  • You may also find this useful. It's from an app I wrote a while ago: https://github.com/indivisible-irl/ClearMeOut/blob/master/src/com/indivisible/clearmeout/DeleteService.java#L62-L151 – indivisible Mar 01 '14 at 10:07