简体   繁体   English

android:写入sdcard失败-为什么?

[英]android: writing to sdcard fails - why?

i have a problem with my code that is supposed to write some data string to my sdcard. 我的代码有问题,应该将一些数据字符串写入我的sdcard。 i use a class to do this: 我使用一个类来做到这一点:

public class CVS {
    private String path;
    private String filename;

    private File dir;
    private File file;

    private FileWriter fw;

    public CVS() {
        path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/traffic/";
        filename = "data.cvs";
        file = new File(path, filename);    
        createDir();
    }

    private void createDir() {
        dir = new File(path);   
        if(!dir.exists()) {
            if(file.mkdirs() == false) {
                Log.d(Config.LOGTAG, "UHOH!!!!!!!!!!!!!!!!!!!!!!!!");
            }
        }
        else Log.d(Config.LOGTAG, "dir exists");
    }

    public void writeToFile(String data) {
        try {
            fw = new FileWriter(file);  
            fw.append(data); Log.d(Config.LOGTAG, "data saved to file...");
        }
        catch(Exception e) {
            Log.d(Config.LOGTAG, "file: " + e.getMessage());
        }
    }
}

this results ALWAYS in an exeption being caught in writeToFile(), saying "permission denied". 这总是导致异常被writeToFile()捕获,并说“权限被拒绝”。 actually, i set permissions to WRITE_EXTERNAL_STORAGE in the manifest. 实际上,我在清单中将权限设置为WRITE_EXTERNAL_STORAGE。 so - what am i doing wrong!? 所以-我在做什么错!!

additional info: real device with sd card mounted. 附加信息:安装了SD卡的真实设备。 no emulator. 没有模拟器。 android 2.2. Android 2.2。 if i create the dir myself, the problem wont go away :( 如果我自己创建目录,问题将不会消失:(

Either: 要么:

  • Your manifest is wrong, or 您的清单错误,或者
  • Your external storage is mounted on your development machine, or 您的外部存储已安装在开发计算机上,或者
  • Your manual concatenation of your directory is wrong 您手动连接目录错误

Your code is ok but still you can add a check for whether sdcard is inserted or not, if you run this code and sdcard is not inserted then it will throw an exception, good practice is that you should always catch the exeptions. 您的代码还可以,但是仍然可以添加是否插入sdcard的检查,如果运行此代码并且未插入sdcard,则会抛出异常,良好的作法是您应该始终捕获异常。 you can check sdcard by following code... 您可以通过以下代码检查sdcard ...

if (android.os.Environment.getExternalStorageState().equals
                (android.os.Environment.MEDIA_MOUNTED))
 {
     //code or logic if sd card is inserted....
 }
 else
 {
     Log.e("Exception","SD Card not found!");
 }

All of the answers are needed, but if it's a Samsung device, then you need to append "/external_sd/" to the path - because they decided they needed to dork with our minds and break the API: 所有答案都是必需的,但如果是三星设备,则需要在路径后附加“ / external_sd /”,因为他们认为自己需要三思而后行并破坏API:

" http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText=sdcard http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText=sdcard

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

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