Write files to external public storage in Android

0 votes

My app should save files to a place where, when you connect your phone/tablet to a computer, you can see them through the system file explorer.

This is the way I implemented file writing:

protected String mDir = Environment.DIRECTORY_DOCUMENTS;
protected File mPath = Environment.getExternalStoragePublicDirectory(mDir);

protected void writeLogFile(String filename) {
    File f = new File(mPath, filename + ".txt");
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(f, false))) {

        // Details omitted.

    } catch (Exception e) {
    makeText("Wrote " + f.getAbsolutePath());

This is what I see when I connect my Sony Xperia Z4 tablet to Windows (notice missing documents folder):

windows file explorer showing tablet contents

This is the directory to which the file is written (using above implementation):

android device monitor showing file system

What is wrong with my implementation?

Mar 1, 2019 in Java by Sushmita
• 6,900 points

1 answer to this question.

0 votes

MediaStore has not discovered your newly-created files yet. What you see in Windows — and in many on-device "gallery" apps — is based on what MediaStore has indexed.

Use MediaScannerConnection and its scanFile() method to tell MediaStore about your file, once you have written out your data to disk:

public void scanFile(Context ctxt, File f, String mimeType) {
        .scanFile(ctxt, new String[] {f.getAbsolutePath()},
                  new String[] {mimeType}, null);
answered Mar 1, 2019 by developer_1
• 3,310 points

Related Questions In Java

0 votes
1 answer

How to unzip files programmatically in Android?

Hello @kartik, This is my unzip method, which ...READ MORE

answered May 25, 2020 in Java by Niroj
• 82,820 points
0 votes
1 answer

How to read text files from the Classpath in Java?

InputStream in = this.getClass().getClassLoader().getResourceAsStream("TextFile.txt"); InputStream in = this.getClass().getResourceAsStream("/TextFile.txt"); package ...READ MORE

answered May 8, 2018 in Java by Akrati
• 3,170 points
0 votes
1 answer

How to read and write on an excel files?

Follow these steps: Write: public class User { ...READ MORE

answered May 15, 2018 in Java by Rishabh
• 3,620 points
0 votes
1 answer

How to run the JAR files in windows?

Following are the steps to run the ...READ MORE

answered May 28, 2018 in Java by Parth
• 4,630 points
0 votes
1 answer

How to Read/Write String from a File in Android

Writing a File in android: private void writeToFile(String ...READ MORE

answered Oct 3, 2018 in Java by sharth
• 3,350 points
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,900 points
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,900 points
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by Parth
• 4,630 points
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,900 points
0 votes
1 answer

How to refresh data in ViewPager Fragment ?

Using the ViewPager.OnPageChangeListener is the correct way to go, ...READ MORE

answered Mar 7, 2019 in Java by developer_1
• 3,310 points