getAbsolutePath()什么都没显示?

时间:2012-03-20 16:02:05

标签: android file-io

我试图通过调用getAbsolutePath()来显示文件的路径,但是应用程序

什么都不显示。

Java代码:

public void createExternalStorageDirectory() {
    File file = new File(getExternalFilesDir(null), fileName);
    try {
        InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        Toast.makeText(getBaseContext(), file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
        is.close();
        os.close();
    } catch (IOException e) {
        Log.w("ExternalStorage", " Error writing " + file, e);
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Environment.getExternalStorageDirectory()而不是getExternalFilesDir(null)

答案 1 :(得分:0)

将外部文件权限添加到清单文件:

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

尝试使用getBaseContext()

的getApplicationContext()intead