0

Here is the code I'm running:

        File root = android.os.Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

        File dir = new File(root.getAbsolutePath());
        dir.mkdirs();
        File file = new File(dir, "myData.txt");

        try {
            FileOutputStream f = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(f);
            pw.println("Hi , How are you");
            pw.println("Hello");
            pw.flush();
            pw.close();
            f.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            android.util.Log
                    .i("tag",
                            "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        } catch (IOException e) {
            e.printStackTrace();
        } 

The code executes without issues, and if I run these tests, all show that file is present on the file system:

        File testFile = new File(dir, "myData.txt");
        Boolean exists = testFile.exists();//true
        Boolean hidden = testFile.isHidden();//false
        String[] files = dir.list();//list contains the file

BUT I can't see the file in File Explorer or Root Browser or Terminal Emulator. AND the list of files queried by dir.list() is different from what I see in Root Browser in /mnt/sdcard/Downloads. I tried searching for the file myData.txt in Terminal Emulator with find / -name *myData* with no results.

I'm running the code on a rooted s4, the AndroidManifest contains WRITE_EXTERNAL_STORAGE permission.

Why is this happening and how do I get the user to see the file after it has been created?

Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224

2 Answers2

3

Perhaps you did not index them with MediaScannerConnection, or perhaps your computer caches the results of the MTP connection to the device.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

My Activity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File root = android.os.Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

    File dir = new File(root.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        android.util.Log
                .i("tag",
                        "******* File not found. Did you add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.Test.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Output: enter image description here

Nirmal
  • 2,340
  • 21
  • 43