How can i create file at external storage in Android

0 votes

 I tried to make 'File Control' app.

I can't find how to write file. I set permission like this.

AndroidManifast.xml

    <application
    ...
    android:requestLegacyExternalStorage="true"
    ...
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

and i call this code in MainActivity.onCreate.

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    public static void verifyStoragePermissions(Activity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }


    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MODE_PRIVATE);
    verifyStoragePermissions(this);

I want read and write file, at /storage/3066-3133/title.txt . /storage/3066-3133 is my sdcard path.

read file is work perfectly.

    public void readFile() {

        String fileTitle = "title.txt";
        File file = new File("/storage/3066-3133", fileTitle);

        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String result = "";
            String line;
            while ((line = reader.readLine()) != null) {
                result += line;
            }
            popupToast("read : " + result);

            reader.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
            popupToast("no file found");

        } catch (IOException e2) {
            e2.printStackTrace();
            popupToast("read fail");
        }

    }

but write code occur exception. write Exception: /storage/3066-3133/title.txt: open failed: EACCES (Permission denied)

    public void writeFile() {
        String fileTitle = "title.txt";
        File file = new File("/storage/3066-3133", fileTitle);

        try {
            if (!file.exists()) {
                file.createNewFile(); // error!!!!
            }
            FileWriter writer = new FileWriter(file, false);
            String str = "      write text     ";
            writer.write(str);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
            popupToast("write fail");
        }
    }

Do i need more permission for sdcard?

or this is problem? First, i create project for target sdk:31. and i change 29 in build.gradle. changing target sdk is not work?

Jun 6, 2022 in Others by polo
• 1,480 points
3,451 views

1 answer to this question.

0 votes

We have not had read/write filesystem access to arbitrary directories on removable storage since Android 4.4. Either use methods like getExternalFilesDirs() on Context, or use the Storage Access Framework (e.g., ACTION_CREATE_DOCUMENT) to let the user choose where to put the content

answered Jun 6, 2022 by nisha
• 2,210 points

Related Questions In Others

0 votes
1 answer

How can I create custom SEO-friendly URLs in OpenCart?

Index: catalog/controller/common/seo_url.php =================================================================== --- catalog/controller/common/seo_url.php (old) +++ catalog/controller/common/seo_url.php ...READ MORE

answered Feb 21, 2022 in Others by narikkadan
• 63,600 points
1,011 views
0 votes
0 answers

How can I re-download the pem file in AWS EC2?

I created a key pair pem file ...READ MORE

Apr 5, 2022 in Others by Kichu
• 19,040 points
2,242 views
0 votes
1 answer

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

You can use OLEDB to create and ...READ MORE

answered Jan 3, 2023 in Others by narikkadan
• 63,600 points
1,604 views
0 votes
1 answer

How can I scrape a excel file from a website and divide it in different parts?

Use Scrapy or beautifulsoup4 parsing data it's more convenient ...READ MORE

answered Jan 13, 2023 in Others by narikkadan
• 63,600 points
668 views
0 votes
1 answer
0 votes
1 answer

Why is collect in SparkR slow?

It's not the collect() that is slow. ...READ MORE

answered May 3, 2018 in Apache Spark by Data_Nerd
• 2,390 points
2,935 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
3,771 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
2,468 views
0 votes
1 answer

Android - How can I find location using just PINCODE?

1 You can find a location (latitude, longitude) ...READ MORE

answered Jun 1, 2022 in Others by nisha
• 2,210 points
1,167 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP