Skip to content Skip to sidebar Skip to footer

Camera Crashes In Android 2.2

I have tested my application on the Android SDK on everything from 1.5 to 2.2 and the camera code in my activity works fine. Running it on a device with 2.1 is also working. But th

Solution 1:

Need to set the preview size of the camera after getting the optimal camera sizes. Here are the details and the code for the fix-

http://code.google.com/p/android/issues/detail?id=7909

Solution 2:

The problem is that the width and height passed by the surfaceChanged method is not a preview size supported.

So if you wanna set the preview size (parameters.setPreviewSize), you need to set a supported preview size. The method getPreviewSize() returns an available preview size. So your surfaceChanged method can be like that:

publicvoidsurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parametersparameters= camera.getParameters();

    SizepSize= camera.getParameters().getPreviewSize();
    Log.d(TAG, "Setting preview size: " + pSize.width + " x " + pSize.height);

    parameters.setPreviewSize(pSize.width, pSize.height);
    camera.setParameters(parameters);
    camera.startPreview();
}

You can also get a list of the supported preview sizes using the method getSupportedPreviewSizes() available from android api version 5.

Solution 3:

You have to take photo from camera then you have to crop.and then save or set.

You can used this code.

Intentintent=newIntent(MediaStore.ACTION_IMAGE_CAPTURE);                  
                mImageCaptureUri = Uri.fromFile(newFile(Environment.getExternalStorageDirectory(),"connectR_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
                try {
                    intent.putExtra("return-data", true);                       
                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }

OnActivityResult.

case PICK_FROM_CAMERA:
            doCrop();               
            break;

now croping the image used doCrop()

final ArrayList<CropOption> cropOptions = newArrayList<CropOption>();      
    Intentintent=newIntent("com.android.camera.action.CROP");
    intent.setType("image/*");        
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );        
    intsize= list.size();        
    if (size == 0) {            
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();         
        return;
    } else {
        intent.setData(mImageCaptureUri);            
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);

        if (size == 1) {
            Intenti=newIntent(intent);
            ResolveInfores= list.get(0);              
            i.setComponent( newComponentName(res.activityInfo.packageName, res.activityInfo.name));                
            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res : list) {
                finalCropOptionco=newCropOption();                 
                co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= newIntent(intent);                   
                co.appIntent.setComponent( newComponentName(res.activityInfo.packageName, res.activityInfo.name));
                cropOptions.add(co);
            }

            CropOptionAdapteradapter=newCropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, newDialogInterface.OnClickListener() {
                publicvoidonClick( DialogInterface dialog, int item ) {
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener( newDialogInterface.OnCancelListener() {
                @OverridepublicvoidonCancel( DialogInterface dialog ) {

                    if (mImageCaptureUri != null ) {
                        getContentResolver().delete(mImageCaptureUri, null, null );
                        mImageCaptureUri = null;
                    }
                }
            } );

            AlertDialogalert= builder.create();

            alert.show();
        }
    }

Post a Comment for "Camera Crashes In Android 2.2"