Camera crashes in Android 2.2

后端 未结 3 1213
感动是毒
感动是毒 2020-12-21 12:10

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.

相关标签:
3条回答
  • 2020-12-21 12:39

    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

    0 讨论(0)
  • 2020-12-21 12:50

    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.

    0 讨论(0)
  • 2020-12-21 12:52

    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();
            }
        }
    
    0 讨论(0)
提交回复
热议问题