Ad Code

Create or Copy File without storage permission Android Studio Java Kotlin | Save file to external storage from Input stream in All Android versions like android 10 11 12 13 without all files access permission and manage external storage permission | How to create a file using Intent

Hello Developer, in this blog I will show you how to copy or create a file in All Android versions like Android 10 11 12 13 without Manage External Storage Permission in Android Studio Java. If you want more helpful Articles and Videos for Android Studio, then Bookmark or site and also subscribe our YouTube channel. We always try to solve users' problems and provide helpful content to them.




Create or Save or Copy File without storage permission Android Studio

Here I have written a simple text file with a string value. You can write any file by bytes or using Input Stream.
Create or Copy File
Button button = findViewById(R.id.button1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                createFile();

            }
        });
Create or Copy File
private void createFile() {


        //You do not to add any permission for saving this file to storage. It will work in all Android version. I have tasted it from Android 8 to Android 12.

        String fileName = "Filename.txt";

        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);

        //You can declare a specific file by myme type

        intent.setType("/");

        intent.putExtra(Intent.EXTRA_TITLE, fileName);

        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);

        startActivityIfNeeded(intent, 1000);


    }

    @Override
    protected void onActivityResult(int requestCode, int _resultCode, @Nullable Intent _data) {
        super.onActivityResult(requestCode, _resultCode, _data);
        if(requestCode == 1000) {
            if(_resultCode == RESULT_OK) {
                Uri uri = _data.getData();

                try {
                    //You can put your file input stream
                    // I will directly write the String bytes

                    String text = "You can use file input stream here.";

                    OutputStream outputStream = getContentResolver().openOutputStream(uri);
                    outputStream.write(text.getBytes());
                    outputStream.close();
                    Toast.makeText(getApplicationContext(), "Saved Successfully : ", Toast.LENGTH_SHORT).show();


                } catch (FileNotFoundException e) {

                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

                } catch (IOException e) {

                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                }


            }



        }
    }

How to copy or create a file using intent in Android Studio?

Here in this method, I have written the code where you can write a file using Intent without any spesific external storage permissions like Write External Storage or Manage External Storage.

Can I create a file using Intent in Android Studio?

Yes, you can write in the storage using Intent. For using this you can use Intent.ACTION_CREATE_DOCUMENT. For using this user need to select a folder from file manager and after selected you can get response in the Activity Result. It will return an Intent and from this intent you can get the Uri. You can write anything in this Uri using Output Stream.

Solution from Google play rejected all files access permission

The only solution is that if your app does not need MANAGE EXTERNAL STORAGE PERMISSION then do not use it in the AndroidManifest.xml file. This permission is only for some rare apps like File Manager, Storage Cleaner etc. If your app is a video player or music player, then use Media Store API. You can also use Intent.ACTION.DOCUMENT.TREE for using a specific folder. If you want to download a file, you can also use "External Storage/Download" folder without storage permission. Hope you understand properly.


For using this method, you do not need to put any permission in the AndroidManifest.xml file. That means without any permission you can write in the external storage. Users need to select the folder from file manager and after selected the File will saved. You can also see the result in OnActivityResult.

Thank you for visiting our website. You can also visit our popular posts. For getting unlimited helpful content Subscribe our YouTube channel now.

Post a Comment

0 Comments

Ad Code