Skip to content

How to save Tempfile to External storage using ACTION_OPEN_DOCUMENT_TREE?

unable to save the zip file to external storage after picking a folder using ACTION_OPEN_DOCUMENT_TREE.

I’m creating a project which creates and manipulate files and document, in that task I want to save that stuff in external storage but I can’t do that with android developer documentation, so please explain additionally.

I want to save this file




what should I do?

here is my code,

– code for picking a directory to save a file into.

saveBinding.btnInternalStorage.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

                startActivityForResult(intent, PICKER_RESULT);

– Code to be executed after selecting a directory,

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            if (requestCode == PICKER_RESULT) {

                if (data != null) {
                    Uri uri = data.getData();
                    String path = uri.getPath();
                    Log.d(TAG, "onActivityResult: uri -> " + uri);
                    Log.d(TAG, "onActivityResult: path -> " + path);


                    new BgExecuter().execute(new Runnable() {
                        public void run() {
                            FileManager.saveFile(PDFTools.this,selectedFile, Uri); //selectedFile is that file that is saved by default in the Temp directory
            }else {
                Toast.makeText(PDFTools.this, "dome thing happen great", Toast.LENGTH_SHORT).show();

method saveFile

public static void saveFile(Activity context, File selectedFile, Uri uri) {

        try {
            InputStream inputStream = context.getContentResolver().openInputStream(Uri.fromFile(selectedFile));
            OutputStream outputStream = context.getContentResolver().openOutputStream(uri);

            // get the content in bytes
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] byteArray = new byte[1024];
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            try {
                byte bytes = (byte);
                if (bytes >= 0) {
                    bufferedOutputStream.write(byteArray, 0, bytes);
            } catch (IOException e) {

        } catch (FileNotFoundException e) {



So Asking for a file instead of directory to be nicer to the user and lessen overwriting issues.

Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

// Create a file with the requested MIME type.
// Suggest a filename
intent.putExtra(Intent.EXTRA_TITLE, "");
// You can suggest a starting directory here see note about this later
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
// OK, this is deprecated and user probably has set their own request code
startActivityForResult(intent, 601);

    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        super.onActivityResult(requestCode, resultCode, resultData);

        if (requestCode == 601 && resultCode == RESULT_OK) {
            // The document selected by the user won't be returned in the intent.
            // Instead, a URI to that document will be contained in the return intent
            // provided to this method as a parameter.
            // Pull that URI using resultData.getData().
            Uri uri;
            if (resultData != null) {
                uri = resultData.getData();
                new BgExecuter().execute(new Runnable() {
                        public void run() {
 // Now that the Uri returned is for a file not a directory, your exist "saveFile" method should work
                            copyFileOut(PDFTools.this, selectedFile, Uri);
//selectedFile is that file that is saved by default in the Temp directory

private Boolean copyFileOut(Context context,File copyFile, Uri uri){

        BufferedInputStream bis;
        BufferedOutputStream bos = null;

        // Now read the file
            InputStream input = new FileInputStream(copyFile);
            int originalSize = input.available();

            bis = new BufferedInputStream(input);
            bos = new BufferedOutputStream(context.getContentResolver().openOutputStream(uri));
            byte[] buf = new byte[originalSize];
            //noinspection ResultOfMethodCallIgnored
            do {
            } while ( != -1);

        } catch (Exception e) {
            if (BuildConfig.LOG) {
                Log.e(Constants.TAG, "copyFileOut:" + e);
            // Notify User of fail
            return false;
        } finally {
            try {
                if (bos != null) {
            } catch (Exception ignored) {
        return true;

Note for initial start directory see How I set the ACTION_OPEN_DOCUMENT_TREE start path the first time a user uses my app?

Also note that in your “saveFile” method

InputStream inputStream = context.getContentResolver().openInputStream(Uri.fromFile(selectedFile));

could be replaced with

InputStream inputStream = new FileInputStream(selectedFile);

As you say that this file is in your Apps private Cache directory, so won’t have any permissions problems.

8 People found this is helpful