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
Related
I need to open a large SQLite Database on Android 11 (api level 30). The Documentation says that "MANAGE_EXTERNAL_STORAGE" is a Problem in the Future.
Therefore, I read the Docs at: https://developer.android.com/training/data-storage/shared/documents-files and used:
public void openDirectory(Uri uriToLoad) {
// Choose a directory using the system's file picker.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == 42
&& resultCode == Activity.RESULT_OK) {
// The result data contains a URI for the document or directory that
// the user selected.
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
DocumentFile dfile = DocumentFile.fromTreeUri(this, uri);
DocumentFile[] fileList = dfile.listFiles();
}
}
}
If I understand the Documentation right, File is deprecated or can not be used with Files on the SD Card and DocumentFile/URI is the new thing. But to open the Database I use:
SQLiteDatabase.openDatabase(
pFile.getAbsolutePath(),
null,
SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY))
So my Problem is: How to open a large (10Gb or more) Database with an URI or a DocumentFile.
Ps.: The Database is a RasterLite File with map images
I am launching a file picker intent. And I am getting a URI of the file from data.getData() method on onActivityResult . There I have a utility class for getting the real path of files from the URI. I want to check whether the path returned from the utility class is valid or not by comparing it with URI. Let assume the utility class returned the path of a different file by mistake. I don't want to use the file name to compare. is there any way?
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==IMAGE_FILE_PICKER_CODE&&resultCode==RESULT_OK){
String path = null;
try {
path = new PathUtils(getActivity()).getPath(data.getData()); //Utility class to get path from uri
File file = new File(path); //want to compare this file with data.getData()
Please Look at the codes below.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
This code is for calling camera function.
After I take a picture, the method onActivityResult gives me back Uri so that I can get the absolute path to internal storage of the picture. But, I want to use the picture just for temporary use. that is, I don't want the picture to be stored in internal storage. I need absolute path to make the picture into a File object. So, is there any way to store the picture on memory allocated to my app??
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != RESULT_OK)
return;
if(requestCode == PICK_FROM_CAMERA){
imageUri = data.getData();
Log.d("메시지", "uri = "+imageUri);
Cursor c = this.getContentResolver().query(imageUri, null, null, null, null);
c.moveToNext();
absolutePath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
}
}
For this you can use Cache Directory:
File storagePath = new File(getCacheDir() + "/" + "YOUR_FILE_NAME.JPG");
I would like to know how to use "Storage Access Framework" to create new folder on SD card. If you give me the code it would be very good.
I have already searched other questions and answers but not found how to.
Add some codes that already work per "CommonsWare" answer.
Note that is the only way I found that it can make new folder in sd card on my phone SS A5 with Android OS 5.1.1
public void newFolder(View view)
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, NEW_FOLDER_REQUEST_CODE);
}
private static final int NEW_FOLDER_REQUEST_CODE = 43;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
Uri currentUri = null;
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == NEW_FOLDER_REQUEST_CODE)
{
if (resultData != null) {
currentUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, currentUri);
DocumentFile newDir = pickedDir.createDirectory("MyFolder");
textView.setText(newDir.getName());
}
}
}
}
You cannot create "create new folder on SD card". You can create a new folder inside some other folder that the user chooses, but you cannot force the user to choose removable storage.
To create a new folder inside of some other folder, this should work:
Start an activity with startActivityForResult() on an ACTION_OPEN_DOCUMENT_TREE Intent, to allow the user to choose a folder. Include FLAG_DIR_SUPPORTS_CREATE to ensure that you can create something new in the folder.
In onActivityResult(), wrap the Uri that you get in a DocumentFile, then call createDirectory() on it to create a new folder as a child of whatever the user chose.
In this other question
How to use the new SD card access API presented for Android 5.0 (Lollipop)?
It is explained how to use the new API to access the "external SDCard".
But, how can I know the actual directory returned in the result activity?
I mean in function
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (resultCode == RESULT_OK) {
Uri treeUri = resultData.getData();
DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
......
How can I get the actual path where "Uri treeUri" points?
Thanks,
Francis.
You can't get the absolute path because the Documents API is designed to abstract this from you.
However, the treeUri does contain the relative path to the file. Try examining the Uri using Uri#getPathSegments()
Use
FileUtil.getFullPathFromTreeUri(treeUri, this)
from
https://github.com/jeisfeld/Augendiagnose/blob/f24ddd7d3da4df94552ca0a9e658399602855a67/AugendiagnoseIdea/augendiagnoseLib/src/main/java/de/jeisfeld/augendiagnoselib/util/imagefile/FileUtil.java
to get the full path. Seperate the path using
final String[] seperated = treeUri.toString().split("\\/");
to get the current dir name.