0

这是我正在运行的代码:

        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();
        } 

代码执行没有问题,如果我运行这些测试,都表明文件系统上存在该文件:

        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

但是我在文件资源管理器或根浏览器或终端模拟器中看不到该文件。并且查询的文件列表与dir.list()我在根浏览器中看到的不同/mnt/sdcard/DownloadsmyData.txt我尝试在终端模拟器中搜索该文件find / -name *myData*,但没有结果。

我在有根的 s4 上运行代码,AndroidManifest 包含 WRITE_EXTERNAL_STORAGE 权限。

为什么会发生这种情况?如何让用户在创建文件后查看文件?

4

2 回答 2

3

也许您没有使用 索引它们MediaScannerConnection,或者您的计算机缓存了与设备的 MTP 连接结果。

于 2013-08-07T16:26:11.747 回答
1

我的活动:

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;
}

}

显现:

<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>

输出: 在此处输入图像描述

于 2013-08-08T05:35:59.980 回答