3

I want to convert File (/storage/A54E-14E9/bp.mp4) to DocumentFile

Uri of DocumentFile should be content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4

but when I use DocumentFile.fromFile(file).getUri() it returns not correct Uri:

file:///storage/A54E-14E9/bp.mp4

So it's different than when you get it in onActivityResult after calling startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), REQUEST_CODE_STORAGE_ACCESS);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_STORAGE_ACCESS && resultCode == RESULT_OK) {
            Uri treeUri = data.getData();
            mDocumentDir = DocumentFile.fromTreeUri(this, treeUri);
            for (DocumentFile file : mDocumentDir.listFiles()) {
                // one of them is content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4
                Log.i(TAG, file.getUri());
            }
        }
    }

or if it's not possible then how to find what equals to File (/storage/A54E-14E9/bp.mp4) in:

// in onActivityResult
for (DocumentFile file : mDocumentDir.listFiles()) {
    Log.i(TAG, file.getUri());
}

it's not good idea to just check file names, I need to check absolute paths (if they equals), also file can be placed in subfodler, so it makes things more difficult

My task is to delete File from MicroSD card, it can be done using DocumentFile after requesting storage (ACTION_OPEN_DOCUMENT_TREE) but I need somehow to get the right DocumentFile which equals my File

user155
  • 775
  • 1
  • 7
  • 25
  • 1
    `Uri of DocumentFile should be content://com.android.externalstorage.documents/tree/A54E-14E9%3A/document/A54E-14E9%3Abp.mp4`. Yes but only if you started with ACTION_OPEN_DOCUMENT_TREE. Not when you picked the file with ACTION_OPEN_DOCUMENT. Please try. – greenapps May 27 '18 at 10:49
  • But then... Why not write a small function that builds the correct uri? Its not that difficult. – greenapps May 27 '18 at 10:51
  • 1
    For the rest it is strange that you start with a file system path to a file you wanna delete. How did you obtain it? But you should not mess around with such paths. You should only use content schemes. – greenapps May 27 '18 at 10:53
  • 1
    @greenapps I just used `listFiles` on a directory to get normal file paths because I use recyclerview (I don't want to use intent tree just to show available files... it would annoy users), anyway I found this hack: https://code.videolan.org/videolan/vlc-android/blob/master/vlc-android/src/org/videolan/vlc/util/FileUtils.java#L296 you can pass `Uri.fromFile(file)` as parameter – user155 Jun 01 '18 at 10:40
  • That is a hack. And in order to let the hack work you have once let the user choose the SD card using ACTION_OPEN_DOCUMENT_TREE. So you could do that before listing 'files' in the recycleview as well. – greenapps Jun 01 '18 at 10:50
  • @greenapps yes I know (at least only once). it's Google fault we're trying to find alternatives because have are we supposed to delete a file (absolute path) from sd card if we can't even convert it easily to documentfile, we of course can show a message to a user which would say: find & delete manually the file you need in that annoying dialog tree, but I don't think he would like to use this difficult way – user155 Jun 01 '18 at 10:53
  • You should list files using the directory content scheme obtained by ACTION_OPEN_DOCUMENT_TREE. Then you do not have to do any conversion. Adapt to modern times! – greenapps Jun 01 '18 at 10:57
  • On Android 7+ you can use StorageVolumes. Then the user does not even have to select the SD card. Only confirm a permission to use it. – greenapps Jun 01 '18 at 10:59
  • @greenapps but we should use File (not DocumentFile) for some directory to get list of video files for example https://lh3.googleusercontent.com/fd-Vx2JsvAkhtzanvExidgNz0dJZ3Y9UJsxl-niXZK1kPe876N44o2oYAH-RZPQ-f0Js=w1920-h949-rw – user155 Jun 01 '18 at 11:14
  • No. Not at all. You can use the Storage Access Framework for all. Try ACTION_OPEN_DOCUMENT_TREE and you see what all you can get: all you see. @user155 – greenapps Jun 01 '18 at 11:15

0 Answers0