简体   繁体   English

在SDCard上写问题-Android

[英]Writing on SDCard Problem - Android

I'm trying to write a file to my SDcard in my HTC Hero phone. 我正在尝试在HTC Hero手机中将文件写入SD卡。 I create the File in my SDCard using: 我使用以下命令在SDCard中创建文件:

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

where path is the path to my externalStorageDirectory (ie \\sdcard) 其中path是我的externalStorageDirectory的路径(即\\ sdcard)

When I log the path of this file, it does say \\sdcard\\filename.txt 当我记录此文件的路径时,它确实说\\ sdcard \\ filename.txt

However, when I create a fileoutputstream to write to the file, suddenly the filepath is changed to \\data\\data and I cannot access it. 但是,当我创建文件输出流以写入文件时,文件路径突然更改为\\ data \\ data,而我无法访问它。

Can someone please help clarify how I can create a File in the SDCard and then write to it? 有人可以帮忙澄清一下我如何在SD卡中创建文件然后对其进行写入吗?

Thanks! 谢谢!

Edit: 编辑:

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

This is what I'm doing. 这就是我在做什么。 The part until SDCARDPLSWORK is correct, but when it comes to the FILEANDROID log it goes to the private data storage. 直到SDCARDPLSWORK的零件都是正确的,但是当涉及到FILEANDROID日志时,它将进入专用数据存储。

使用Environment.getExternalStorageDirectory()作为SD卡路径。

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

getFileStreamPath(..) reads from local directory. getFileStreamPath(..)从本地目录读取。 Your debug code is wrong. 您的调试代码是错误的。

Environment.getExternalStorageDirectory() will get the root folder of sdcard. Environment.getExternalStorageDirectory()将获取sdcard的根文件夹。

@JAVADOC @JAVADOC

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace. 应用程序不应直接使用此顶级目录,以避免污染用户的根名称空间。 Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. 应将应用程序专用的所有文件放在Context.getExternalFilesDir返回的目录中,如果卸载了应用程序,系统将负责删除该目录。 Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String). 其他共享文件应放在getExternalStoragePublicDirectory(String)返回的目录之一中。

/data/data/... this location is used to store files for your package, so try the codes below: / 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM