2

Which day I struggle with the problem and have already read the entire Internet. I'm using the Storage Access Framework but can't delete a file in a subdirectory.

There is a folder on the memory card (/storage/1B15-0D11/Data). I am asking the user for permissions on this folder:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 999);

public void onActivityResult(int requestCode, int resultCode,
                         Intent resultData) {
 if (resultCode == Activity.RESULT_OK) {
    if (requestCode == 999) {
        if (resultData != null) {
            Uri treeUri=resultData.getData();
            //Log.d(TAG, "SELECT_DIR_REQUEST_CODE resultData = " + resultData);
            getContentResolver().takePersistableUriPermission(treeUri, (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION));

Structure of this folder:

Data->Data1->Data2->file.txt

I know that there is a file (file.txt) on the /storage/1B15-0D11/Data/Data1/Data2 path.

But I don’t know how to remove it without going through all the subfolders.

Can be removed like this:

List<UriPermission> permissions = getContentResolver().getPersistedUriPermissions();
if (permissions != null && permissions.size() > 0) {
   DocumentFile dir = DocumentFile.fromTreeUri(this, permissions.get(0).getUri());
   DocumentFile dir2 = dir.findFile("Data1");
   DocumentFile dir3 = dir2.findFile("Data2");
   DocumentFile file = dir3.findFile("file.txt");
   if (file != null) {file.delete();}
}

It works. But that's not exactly what I need.

I have a file path in the form "/storage/1B15-0D11/Data/Data1/Data2/file.txt". How can I remove it without going through all the subfolders?

Tried like this. But that doesn't work. It turns out not the correct line URI.

DocumentFile dir = DocumentFile.fromTreeUri(this, Uri.parse("content://com.android.externalstorage.documents/tree/1B15-0D11%3AData%2FDat1%2FDat2"));
DocumentFile file = dir.findFile("text.txt"); //return null


                
Alex
  • 43
  • 3
  • `It works. But that's not exactly what I need` Why not? It is exactly what you need i would say. Interesting question by the way! – blackapps Jan 25 '22 at 12:59
  • 1
    If you use DocumentsContract you can get a docId for the original folder. Then concatenate the subfolders all at once and the filename which gives a new docId for which you can get an uri. Well about that way.. – blackapps Jan 25 '22 at 13:14
  • Because the work goes on with more than one file and the main work in the program goes with the help of: File dir = new File(curPath); String[] list = dir.list(); and in SAF completely different addressing. And the user can call for deletion of any file. And I still don’t quite understand how to reflect one on the other correctly. And the option of walking through subdirectories seems not quite correct. Besides I will not think yet how to organize it dynamically for any file. Documents Contract? I'll try to read on it what it is and how to work with it. – Alex Jan 25 '22 at 13:59
  • Parsing works correctly if you collect the line with the file name in full. Then it can be removed. If you collect the line only up to the directory and then look for the desired file there, this does not work. I will use it like this. – Alex Feb 02 '22 at 07:08

2 Answers2

0

Easier to just add the findFile to delete.

if (dir3.findFile("file.txt") !=
null) {
dir3.findFile("file.txt").delete();}
J. M.
  • 115
  • 6
0

Try this.

public void deleteAPI29(ArrayList<Media> mediaList) {
        Uri persistedUri = getContentResolver().getPersistedUriPermissions().get(0).getUri();
        DocumentFile documentFile = DocumentFile.fromTreeUri(this, persistedUri);
        for (int i = 0; i < mediaList.size(); i++) {
            File file = new File(mediaList.get(i).getPath());
            DocumentFile nextDocument = documentFile.findFile(file.getName());
            try {
                DocumentsContract.deleteDocument(getContentResolver(), nextDocument.getUri());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
Dev4Life
  • 2,328
  • 8
  • 22