写在SDCard问题 - Android

时间:2010-12-01 08:00:23

标签: java android

我正在尝试将文件写入我的HTC Hero手机中的SD卡。我使用以下方法在SDCard中创建文件:

File = new File(path.getAbsolutePath(),“Filename.txt”);

其中path是我的externalStorageDirectory(即\ sdcard)的路径

当我记录此文件的路径时,它会说\ sdcard \ filename.txt

但是,当我创建一个fileoutputstream来写入文件时,文件路径突然变为\ data \ data而我无法访问它。

有人可以帮助澄清我如何在SD卡中创建文件然后写信给它吗?

谢谢!

编辑:

path = Environment.getExternalStorageDirectory();
Log.d("SDCARDPLSWORK", path.toString());
    try
    {
        myFile = new File(path.getAbsolutePath(), "SensorValues.txt");
        boolean i = myFile.createNewFile();
        Log.d("SDCARDPLSWORK", myFile.toString() + " " + i);
        fos = new FileOutputStream(myFile);
        Log.d("FILEANDROID", getFileStreamPath("SensorValues.txt").toString());
    }

    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }

这就是我正在做的事情。直到SDCARDPLSWORK正确的部分,但是当涉及到FILEANDROID日志时,它会进入私有数据存储。

3 个答案:

答案 0 :(得分:0)

对SD卡路径使用 Environment.getExternalStorageDirectory()

Environment.getExternalStorageDirectory()+"/yourFilename.png"

答案 1 :(得分:0)

getFileStreamPath(..)从本地目录读取。您的调试代码错误。

答案 2 :(得分:0)

Environment.getExternalStorageDirectory()将获取sdcard的根文件夹。

@JAVADOC

  

应用程序不应直接使用   这个顶级目录,以便   避免污染用户的根   命名空间。任何私有的文件   应该放在应用程序中   返回的目录   Context.getExternalFilesDir,其中   系统将负责删除if   应用程序已卸载。其他   共享文件应放在一个文件中   返回的目录   getExternalStoragePublicDirectory(字符串)。

/ data / data / ...此位置用于存储包裹的文件,请尝试以下代码:

File path = getApplicationContext().getFilesDir(); // get your private folder
Log.d("SDCARDPLSWORK", path.toString());
File myFile;
FileOutputStream fos;
try {
    myFile = new File(path.getAbsolutePath(),
            "SensorValues.txt");
    boolean i = myFile.createNewFile();
    Log.d("SDCARDPLSWORK", myFile.toString() + " " + i);
    fos = new FileOutputStream(myFile);
    Log.d("FILEANDROID", getFileStreamPath("SensorValues.txt")
            .toString());
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
相关问题