Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

android - camera.takePicture() is crashing my app

I have an app that is supposed to take pictures using the camera.takePicture. The code I use is the following :

private Bitmap bitmapPicture;
//inside onCreate
btn.setOnClickListener(new OnClickListener(){

    public void onClick(View v){
        camera.takePicture(myShutterCallback, 
                myPictureCallback_RAW, myPictureCallback_JPG);
    }
    });


 //inside the activity
 ShutterCallback myShutterCallback = new ShutterCallback(){

@Override
public void onShutter() {
    // TODO Auto-generated method stub

}};

PictureCallback myPictureCallback_RAW = new PictureCallback(){

@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
    // TODO Auto-generated method stub

}};

PictureCallback myPictureCallback_JPG = new PictureCallback(){

@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
    // TODO Auto-generated method stub
    bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
}};

When i click the button the shutter sound plays, the image on the surfaceView frozes but then the app crashes . Why is this happening ? When i run it on the emulator with an emulated camera its working but on a device is crashing.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this simple code to capture using the device camera

IMP Note: Add those permision to the mainfest file

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

The mainActivity

public class MainActivity extends Activity {

private static final int CAMERA_PIC_REQUEST = 1111;//Constant ID for ActivityResult
private ImageView mImage; // To display the thumbnail

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mImage = (ImageView) findViewById(R.id.camera_image);

    // Start an intent for Camera Capture with ResultActivity
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_PIC_REQUEST) {
        // Get the image in a Bitmap extension to assign it to the ImageView
        if (data.getExtras() == null)
            return;
        Bitmap thumb = (Bitmap) data.getExtras().get("data");
        mImage.setImageBitmap(thumb);

        // Compress the Bitmap image into JPEG to save it on the device
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, bytes);// 100 is the scale..for less quality decrease the number                                

        // Save the image on the root SDCard
        File file = new File(Environment.getExternalStorageDirectory()
                + File.separator + "imageName.png");

        try {
            // Create the file to save the image
            file.createNewFile();
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (Exception e) {
        }

    }

}

}

Put This in your Layout MainActivity.XML :

    <ImageView android:id="@+id/camera_image"
    android:layout_width="wrap_content" android:layout_height="wrap_content" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...