1

I am trying to take a photo using the front camera. After the button is clicked, the application takes a photo and saves it without displaying the photo and camera view.

camera.takePicture does not work, I got this error:

Caused by: java.lang.RuntimeException: takePicture failed

What is wrong with the following code?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.os.Bundle;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
    Button button = (Button) findViewById(R.id.buttonCapture);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            camera.takePicture(null, null, mPicture);
        }
    });

}

private Camera.PictureCallback mPicture = new Camera.PictureCallback(){
    @Override
    public void onPictureTaken(byte[] data, Camera camera){
        File pictureFile = getOutputMediaFile();

        if(pictureFile == null){
            Log.d("TEST", "Error creating media file, check storage permissions");
            return;
        }
        try{
            Log.d("TEST","File created");
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        }catch(FileNotFoundException e){
            Log.d("TEST","File not found: "+e.getMessage());
        } catch (IOException e){
            Log.d("TEST","Error accessing file: "+e.getMessage());
        }
    }
};

private File getOutputMediaFile(){

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");

    if(!mediaStorageDir.exists()){
        if(!mediaStorageDir.mkdirs()){
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");

    return mediaFile;
}}

Permissions:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
ffttyy
  • 853
  • 2
  • 19
  • 49
  • add the stack trace, and have you added use-permission? – Derek Fung Sep 04 '15 at 14:18
  • @DerekFung I am new and I looked but don't understand how to add stack trace. Please send me some source or example for beginners about it, if you know. I added permissions I used to the question. – ffttyy Sep 04 '15 at 17:14
  • stack trace means the full log of your exception, `Caused by: java.lang.RuntimeException: takePicture failed` is only part of the full stack trace. – Derek Fung Sep 04 '15 at 17:17
  • http://stackoverflow.com/questions/32382507/can-not-perform-this-action-after-onsaveinstancestate-on-super-onbackpressed/32390746#32390746, check this post, example of full stack trace – Derek Fung Sep 04 '15 at 17:20

0 Answers0