Issues traversing through directory hierarchy with Android Storage Access Framework / DocumentProvider using MTP

I am using your code to go in sub folders with adding lines: Uri childrenUri; try { //for childs and sub child dirs childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri)); } catch (Exception e) { // for parent dir childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri)); }

Open a Google Drive File Content URI after using KitKat Storage Access Framework

Ok. I found that the right way is to use the input stream from the other posts in conjunction with some data from the contentresolver. For reference here are the hard to find android docs: https://developer.android.com/training/secure-file-sharing/retrieve-info.html The relevant code to get mimetype, filename, and filesize: Uri returnUri = returnIntent.getData(); String mimeType = getContentResolver().getType(returnUri); Cursor returnCursor … Read more

How to persist permission in android API 19 (KitKat)?

I believe I’ve solved it. The request intent: Intent intent; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); }else{ intent = new Intent(Intent.ACTION_GET_CONTENT); } intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setType(“image/*”); startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.form_pick_photos)), REQUEST_PICK_PHOTO); and onActivityResult … // kitkat fixed (broke) content access; to keep the URIs valid over restarts need to persist access permission if(Build.VERSION.SDK_INT … Read more

How to get free and total size of each StorageVolume?

Is getAllocatableBytes indeed the way to get the free space? Android 8.0 Features and APIs states that getAllocatableBytes(UUID): Finally, when you need to allocate disk space for large files, consider using the new allocateBytes(FileDescriptor, long) API, which will automatically clear cached files belonging to other apps (as needed) to meet your request. When deciding if … Read more

Android SD Card Write Permission using SAF (Storage Access Framework)

Letting the user choose the “SD card” or even the “Internal storage” SAF root give your application access to the corresponding storage, but only through the SAF API, not directly via the filesystem. For example you code could be translated into something like : public void writeFile(DocumentFile pickedDir) { try { DocumentFile file = pickedDir.createFile(“image/jpeg”, … Read more

Android SAF (Storage Access FrameWork): Get particular file Uri from TreeUri

Access Sd-Card’s files Use DOCUMENT_TREE dialog to get sd-card’s Uri. Inform the user about how to choose sd-card on the dialog. (with pictures or gif animations) // call for document tree dialog Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE); On onActivityResult you’ll have the selected directory Uri. (sdCardUri) @Override protected void onActivityResult(int requestCode, int resultCode, … Read more