tags:

views:

63

answers:

3

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 directory 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.

A: 

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).

Yoni Samlan
While that may be an issue further down the road.. Currently the journid is input from the user. I tried out one with out spaces still the same thing<pre>08-23 14:00:00.223: WARN/System.err(9515): java.io.FileNotFoundException: /sdcard/GrowJournalPhotos/be/3/photo.jpg</pre>
Brian
What happens if you try to mkdir those directories from an ADB shell connected to your device/emulator?
Yoni Samlan
A: 
  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.

radek-k
Thanks. works how i would expect. I've changed my code above. Let me know if anything is off.
Brian
A: 

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

Falmarri
I stated in the last paragraph, first sentence that i had already done this. Thanks for the input though
Brian