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 in Others by polo
• 1,500 points
27 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 by nisha
• 2,160 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 in Others by narikkadan
• 10,840 points
32 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 in Others by Kichu
• 19,040 points
169 views
0 votes
1 answer

How can you create an object in JavaScript?

JavaScript supports Object concept very well. You can create ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
182 views
0 votes
1 answer

How can you create an Array in JavaScript?

You can define arrays using the array literal as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
238 views
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
1,184 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
2,960 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,080 points
1,514 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! An example via Matt Cutts via SL4A -- "here’s ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,080 points
391 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 in Others by nisha
• 2,160 points
40 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP