Sunday, 5 January 2020

Create File on External Storage using Storage access framework for android 10

How To Create File on External Storage on android 10 using Storage Access Framework?


By default, apps targeting Android 10 and higher are given scoped access into external storage, or scoped storage. So from android 10 onward you don't allow to use Environment.getExternalStorageDirectory() for creating a file on external storage instate of this either create a file using the Storage Access Framework (Eg.ACTION_OPEN_DOCUMENT_TREE ) or create a file on the app-specific directory. For creating a file using the storage access framework you don't need to give read-write permission in the Manifest file. below steps give you a quick idea of how to use the Storage Access Framework.

1. Call Below intent which will open system dialog to choose the location for creating directory or file on external storage.

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


2. On Activity, result get URI and Create DocumentFile object and use DocumentFile object to create directory, file or getting a list of files. Uri found in onActivityResult can be store and reuse if the user wants to create a file/Directory on the Same Location.


@Override

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent resultData) {

        super.onActivityResult(requestCode, resultCode, resultData);

        if (requestCode == 42) {

            if (resultCode == RESULT_OK) {

                Uri treeUri = resultData.getData();

                pickedDir = DocumentFile.fromTreeUri(this, treeUri);

                DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);


                // List all existing files inside picked directory

                for (DocumentFile file : pickedDir.listFiles()) {

                    Log.d(TAG, "Found file " + file.getName() + " with size " + file.length());

                }

            }

        }

    }


3. The following code shows how to create a File or directory.

if (pickedDir != null) {
                    DocumentFile documentFile = pickedDir.findFile("My Space");
                    if (documentFile == null)
                        documentFile = pickedDir.createDirectory("My Space");
                    DocumentFile newFile = documentFile.createFile("image/jpg", "My Car");
                    if (newFile==null)
                        textView.setText("Unable to create File");
                    else
                        textView.setText("File has been successfully created");

                } else {
                    textView.setText("Unable to create File");
                }

Demo: Check the demo here

Ref. link
  1. https://developer.android.com/about/versions/10/privacy/changes#scoped-storage
  2. https://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT_TREE
  3. https://developer.android.com/guide/topics/providers/document-provider
Screenshot:


No comments:

Post a Comment

Create File on External Storage using Storage access framework for android 10

How To Create File on External Storage on android 10 using Storage Access Framework? By default, apps targeting Android 10 and higher a...