开发者

IOException when trying to access SD card from android

everyone. I'm trying to save a picture with android to the public pictures directory on the sd card. However, I can't seem to get as far as opening a file for writing. Here is the code:

                boolean mExternalStorageWriteable = false;
            String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)) {

               mExternalStorageWriteable = true;
            } 

            if(mExternalStorageWriteable){
                    File root_dir = Environment.getExternalStorageDirectory();
                    root_dir.mkdirs();
                    File pictures_directory = new File(root_dir, "/Pictures/");
                    pictures_directory.mkdirs();
                    File outputfile = new File(pictures_directory, "test_file.jpg");
                    try {
                        outputfile.createNewFile();
                        FileOutputStream out = new FileOutputStream(outputfile);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }

My debugger always jumps to the catch block (caused by the createNewFile call) and the IOExceptions message is "Not a Directory". If I remove the createNewFile call, the FileOutPut开发者_开发知识库Stream constructor will throw a FileNotFound exception. Opening a fileoutput stream to the root of the file system or the pictures directory seems to work fine however.

To dispense with the obvious, my manifest includes the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> tag. I'm using API level 5 and my emulator has a virtual SD card mounted to it. Any suggestions are greatly appreciated.

SOLVED: There was a problem with the emulator filesystem. DDMS showed /Pictures/ being created as a file or a directory for some reason. Creating a new emulator solved the issue.


That's usually a bad manner to hard coder path separators. Try just Pictures instead of /Pictures/. The last one is an absolute path which point to a folder in root:

File pictures_directory = new File(root_dir, "Pictures");

UPD:

I have compiled and run your code snippet with no problems. So the problem is in your emulator file system. Try to remove that Pictures file first, or even create a new sdcard image for your emulator.


Your confusing the following two methods:

File(String Path) and File(File dir, String name)

As such, pictures_directory should be a directory not a complete path + filename.

Try changing

File pictures_directory = new File(root_dir, "/Pictures/");

to

File pictures_directory = new File(root_dir + "/Pictures/");


Try this method. Make sure you have test_file.jpg in your res/drawable directory.

    private void writeToSDCard() {
    try {
        File root = Environment.getExternalStorageDirectory();

        if (root.canWrite()){
            InputStream from = getBaseContext().getResources().openRawResource(R.drawable.test_file);
            File dir = new java.io.File (root, "Pictures");
            dir.mkdir();
            File writeTo = new File(root, "Pictures/test_file.jpg");
            FileOutputStream  to = new FileOutputStream(writeTo);

            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead); // write
            to.close();             
            from.close();
        } else {
            Log.d("MyActivity", "Unable to access SD card.");
        }
    } catch (Exception e) {
        Log.d("MyActivity", "writeToSDCard: " + e.getMessage());
    }
}   "
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜