Camera crashes in Android 2.2

我们两清 提交于 2019-11-29 15:44:07

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

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:

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Camera.Parameters parameters = camera.getParameters();

    Size pSize = 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.

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

You can used this code.

                Intent intent   = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                  
                mImageCaptureUri = Uri.fromFile(new File(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 = new ArrayList<CropOption>();      
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setType("image/*");        
    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );        
    int size = 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) {
            Intent i        = new Intent(intent);
            ResolveInfo res = list.get(0);              
            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));                
            startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
            for (ResolveInfo res : list) {
                final CropOption co = new CropOption();                 
                co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= new Intent(intent);                   
                co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                cropOptions.add(co);
            }

            CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                public void onClick( DialogInterface dialog, int item ) {
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                }
            });

            builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel( DialogInterface dialog ) {

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

            AlertDialog alert = builder.create();

            alert.show();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!