0

I have an app that uses the Camera API, and it takes a photo with the call

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

mCamera.takePicture(shutterCallback, rawCallback, mPicture);
mCamera.startPreview();

mPicture is defined as

private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: " /* +
                    e.getMessage()*/);
            Toast.makeText(MainActivity.this, "Failed to write Photo to File", Toast.LENGTH_SHORT);
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            Toast.makeText(MainActivity.this, "Writing Photo to File", Toast.LENGTH_SHORT);
            fos.write(data);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};


public static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.



    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/camtest");
    dir.mkdirs();

    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        mediaFile = new File(dir, "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }
    return mediaFile;
}

Why isn't any pictures saved? I went to my gallery & file manager app to check, nothing. I also noticed that my app crashes when I exit the app, could that be why?

My onPause method is as follows

@Override
protected void onPause() {
    mSensorManager.unregisterListener(mShakeDetector);
    letGo();
    super.onPause();
}
private void letGo(){
    if(mCamera != null){
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        cameraPreview.getHolder().removeCallback(cameraPreview);
        mCamera.release();
        mCamera = null;
    }
}
Jasmine Rain
  • 419
  • 2
  • 6
  • 17

1 Answers1

0

Why isn't any pictures saved?

It is entirely possible that they are being saved. However, bear in mind that you are doing disk I/O on the main application thread; depending on StrictMode settings, you may be crashing due to this I/O.

If the pictures are not being saved, you should be seeing your log messages in LogCat. I recommend switching them to error severity (Log.e()), though.

I went to my gallery & file manager app to check, nothing.

They will not show up in a device "gallery"-style app, or in your desktop OS's file manager, for quite some time. That is because those tools use the MediaStore to see what exists, and you have not arranged to have your file be indexed by the MediaStore. To do that, use MediaScannerConnection and its scanFile() method.

I also noticed that my app crashes when I exit the app

Use LogCat to examine the Java stack trace associated with your crash.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for your answer, it turns out that it was saved all along, but didn't show up on my Gallery app, eventually they did. Can you expand on how to use the MediaScannerConnection.scanFile()? – Jasmine Rain Feb 01 '16 at 21:19
  • @JasmineRain: "but didn't show up on my Gallery app, eventually they did" -- external storage is scanned periodically for changes, updating the `MediaStore`, but you don't know when. "Can you expand on how to use the MediaScannerConnection.scanFile()? " -- um, it's a static method. Call it, passing in details of the file that you wrote. It will arrange to get your file added to the `MediaStore`. While that will happen asychronously, it's usually pretty fast -- by the time the user goes looking for the file, it should be indexed. – CommonsWare Feb 01 '16 at 21:27
  • "you may be crashing due to this I/O" I think this is the cause of my crash, can you kindly tell me how to fix this issue? – Jasmine Rain Feb 02 '16 at 02:27
  • @JasmineRain: Have `onPictureTaken()` save the picture to a file using a background thread. – CommonsWare Feb 02 '16 at 11:28