问题
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
来源:https://stackoverflow.com/questions/50550913/convert-file-to-documentfile