简体   繁体   中英

How to delete file from removable sd card (second external storage)?

I am trying to delete a file (PATH_TO_SD_CARD/DCIM/1.PDF) on android 9 from my sd card (removable).

1. I get the volumePaths where the 2nd one is my sd card.

private static String[] getVolumePaths(Context context) {
    String[] volumes = null;
    StorageManager managerStorage = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    if (managerStorage == null) {
        return volumes;
    }
    try {
        return (String[]) managerStorage.getClass().getMethod("getVolumePaths", new Class[0]).invoke(managerStorage, new Object[0]);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return volumes;
    } catch (IllegalArgumentException e2) {
        e2.printStackTrace();
        return volumes;
    } catch (IllegalAccessException e3) {
        e3.printStackTrace();
        return volumes;
    } catch (InvocationTargetException e4) {
        e4.printStackTrace();
        return volumes;
    }
}

    String[] volumePaths = getVolumePaths(getContext());
    Log.v("VOLUME1: ", "-> "+volumePaths[0]);
    Log.v("VOLUME2: ", "-> "+volumePaths[1]);

LocCat:

2019-07-19 17:06:15.577 13257-13257/com.spicysoftware.spicypdfreader V/VOLUME2:: -> /storage/2585-1513

2. I check if the file exists which returns true

Log.v("FILE EXISTS?", "-> "+new File("/storage/2585-1513/DCIM/1.PDF").exists());

LogCat

2019-07-19 17:06:16.577 13257-13257/com.spicysoftware.spicypdfreader V/FILE EXISTS? -> true

3.1 I am trying to delete the file

But this way does not work, why?

new File("/storage/2585-1513/DCIM/1.PDF").delete();

3.2 I am trying to delete the file via DOCUMENT_FILE_TREE

// call for document tree dialog
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 123);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 123:
            if (resultCode == Activity.RESULT_OK) {
                String strFilePath = data.getData().getPath()+"/DCIM/1.PDF";
                try {
                    if(new File(strFilePath).delete()){
                        Log.v("FILE", "DELETED");
                    }else{
                        Log.v("FILE", "NOT DELETED");
                    }
                }catch (Exception e){
                    Log.v("ERROR: ", "-> "+e);
                }
            }
            break;
    }
}

LogCat

2019-07-19 17:23:20.523 14343-14343/com.spicysoftware.spicypdfreader V/FILE: NOT DELETED

So my question is: How to delete file from removable sd card (second external storage)? This is so weird... I was 2 days on this problem and didn't find any solution how to delete a file on this sd card...

I also used the storage access framework which did not work. If it is the only way to handle that pls add an example.

How to delete file from removable sd card (second external storage)?

You don't. You do not have read/write access to arbitrary locations on removable storage.

I also used the storage access framework which did not work

Not really. You used ACTION_OPEN_DOCUMENT_TREE to get a Uri , then tried to turn it into a File . That will not work, as a Uri is not a file.

The simplest solution is:

  • Use ACTION_OPEN_DOCUMENT

  • Pass the Uri that you get ( data.getData() from your question) and pass that to DocumentFile.fromSingleUri()

  • Call delete() on that DocumentFile

By security, you can access to your SD Card but you can only write & read files into the directory that was created for your app or files inside the directories created by your app (using WRITE_EXTERNAL_STORAGE permission).

在此输入图像描述

for example if your external storage path is

/storage/13257-13257/

and your app package name is com.spicysoftware.spicypdfreader

You can access and delete files created inside this path:

 /storage/13257-13257/com.spicysoftware.spicypdfreader

By the way i suggest to you use ContextCompat.getExternalFilesDirs(context, null) to get the path of your /files directory /storage/13257-13257/com.spicysoftware.spicypdfreader/files .

Example:

 File[] storages = ContextCompat.getExternalFilesDirs(context, null);
Log.i(TAG, "storage[0]: " + storages[0]);
Log.i(TAG, "storage[1]: " + storages[1]);

in conclusion, you can delete files here:

new File("/storage/13257-13257/com.spicysoftware.spicypdfreader/files/1.PDF").delete();

or here (outside /files directory):

new File("/storage/13257-13257/com.spicysoftware.spicypdfreader/1.PDF").delete();

but is not possible to do this:

new File("/storage/13257-13257/DCIM/1.PDF").delete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM