开发者

Directory creation failing... Java, Android

I'm trying to save photos that I taken with my app to a specific directory. It was working but I'm getting close to being finished with the app and wanted to try a fresh install. I deleted the folder where the photos were being saved and now it won't remake them. Here's my code.

 PictureCallback jpegCallback = new PictureCallback() { // 
     public void onPictureTaken(byte[] data, Camera camera) {
       FileOutputStream outStream = null;
       try {      
                 android.os.Environment.getExternalStorageState();
            // create a File object for the parent directory
              File PhotoDirectory = new File(
                      android.os.Environment.getExternalStorageDirectory()+
                      "/GrowJournalPhotos/"+journ_id+"/"+plant_id+"/");
        // have the object build the direc开发者_运维百科tory structure, if needed.
        PhotoDirectory.mkdirs();
        // create a File object for the output file
        File outputFile = new File(PhotoDirectory, "photo.jpg");
        // now attach the OutputStream to the file object, instead of a String representation
        outStream = new FileOutputStream(outputFile);    //----LINE 82----//

         // Write to SD Card
         outStream.write(data);
         outStream.close();
         Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
         setResult(RESULT_OK);
            finish();

       } catch (FileNotFoundException e) { // 
         e.printStackTrace();
       } catch (IOException e) {
         e.printStackTrace();
       } finally {
       }
       Log.d(TAG, "onPictureTaken - jpeg");
     }
   };

And the IO error:

08-23 13:26:18.263: WARN/System.err(9515): java.io.FileNotFoundException: /sdcard/GrowJournalPhotos/geo a/1/photo.jpg
08-23 13:26:18.263: WARN/System.err(9515):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
08-23 13:26:18.263: WARN/System.err(9515):     at java.io.FileOutputStream.(FileOutputStream.java:97)
08-23 13:26:18.263: WARN/System.err(9515):     at java.io.FileOutputStream.(FileOutputStream.java:69)
08-23 13:26:18.263: WARN/System.err(9515):     at com.grower.beta.takephoto$3.onPictureTaken(takephoto.java:82)
08-23 13:26:18.263: WARN/System.err(9515):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:323)
08-23 13:26:18.263: WARN/System.err(9515):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 13:26:18.263: WARN/System.err(9515):     at android.os.Looper.loop(Looper.java:123)
08-23 13:26:18.263: WARN/System.err(9515):     at android.app.ActivityThread.main(ActivityThread.java:4595)
08-23 13:26:18.263: WARN/System.err(9515):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 13:26:18.263: WARN/System.err(9515):     at java.lang.reflect.Method.invoke(Method.java:521)
08-23 13:26:18.263: WARN/System.err(9515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-23 13:26:18.263: WARN/System.err(9515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-23 13:26:18.263: WARN/System.err(9515):     at dalvik.system.NativeStart.main(Native Method)
08-23 13:26:18.263: DEBUG/CameraDemo(9515): onPictureTaken - jpeg

I do have permissions set to write to external storage. If I create the directory manually, mounting as drive on computer and creating the first folder "/sdcard/GrowJournalPhotos/" everything else will work. Defiantly need to make sure the app will create the entire directory, when necessary.


  1. SD card does not have to bemounted as "/sdcard" path. Always use android.os.Environment.getExternalStorageDirectory()

  2. Before writing to SD card check its state with android.os.Environment.getExternalStorageState()

  3. Make sure all directories are created before creating FileOutputStream instance. In your case this would be: PhotoDirectory.mkdirs(). Check returned value to be sure all directories in a path exist.


Is that a space I see in the directory name? Don't do that, or if you really have to, properly unix-escape them (with backslashes).


Make sure you have the android.permission.WRITE_EXTERNAL_STORAGE declared in your manifest


You must ask for permissions at runtime as well as declare the following permission in the manifest file.

android.permission.WRITE_EXTERNAL_STORAGE
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜