捕获图像时在后端摄像头中出现问题

时间:2016-06-17 13:10:18

标签: android camera

问题定义:这里我尝试开发一个应用程序,它是从库中选择图像,而用户必须单击按钮并打开选项菜单,如“捕获图像”和“从库中选择”,并从任一库中选择图像或通过相机捕捉。我尝试以编程方式显示所选图像集按钮左侧(按钮左侧可绘制属性)。虽然我试图从前置摄像头捕获图像,然后它的工作正常。但是当我尝试使用后置(后置)相机的时候我的应用程序会崩溃.. 我该怎么做才能使用两个相机(前+后)?

What i needed in output from back camera on button click open opt menu

private static final int CAMERA_REQUEST_CLOSEUP_SMILE = 1;
private static final int PICK_FROM_GALLERY_CLOSEUP_SMILE = 2;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        final String[] option = new String[] { "Take from Camera","Select from Gallery" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, option);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("Select Option");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Log.e("Selected Item", String.valueOf(which));
                if (which == 0) {
                    callCamera();
                }
                if (which == 1) {
                    callGallery();
                }

            }
        });
        final AlertDialog dialog = builder.create();
        btnCloseUpSmile.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {
                dialog.show();
               }
        });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK)
        return;
switch (requestCode) {

        case CAMERA_REQUEST_CLOSEUP_SMILE:

        case PICK_FROM_GALLERY_CLOSEUP_SMILE:
            Bundle extras2 = data.getExtras();
            Uri filePath = data.getData();

            Bitmap bCloseUpSmile=getPath(data.getData());

            int widthCloseUp = bCloseUpSmile.getWidth();
            int heightCloseUp = bCloseUpSmile.getHeight();
            int newWidth = (btnCloseUpSmile.getHeight()/2);
            int newHeight = (btnCloseUpSmile.getHeight()/2);

            // calculate the scale - in this case = 0.4f
            float scaleWidth = ((float) newWidth) / widthCloseUp;
            float scaleHeight = ((float) newHeight) / heightCloseUp;

            Matrix matrixCloseUp = new Matrix();
            matrixCloseUp.postScale(scaleWidth, scaleHeight);

            Bitmap resizedBitmapCloseUp = Bitmap.createScaledBitmap(bCloseUpSmile, 36, 36, true);
            Drawable d=(new BitmapDrawable(getResources(), resizedBitmapCloseUp));
            bitmapForCloseUpSmile = resizedBitmapCloseUp;
            boolCloseUpSmile=true;
            btnCloseUpSmile.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
            selectedImageCloseUpSmile=getStringPathImage(data.getData());
            System.out.println("File Path = "+selectedImageCloseUpSmile);

        break;
}


private Bitmap getPath(Uri uri) {

        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        // Convert file path into bitmap image using below line.
        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    return bitmap;
}

/**
 * open camera method
 */
// ------------------------------ With Smile Reuest------------------------------------------------------------------
public void callCamera() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, CAMERA_REQUEST_CLOSEUP_SMILE);
}

    /**
     * open gallery method
     */
public void callGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY_CLOSEUP_SMILE);
}
//----------------------------------------------------------------------------------------------------------------------

崩溃日志(错误):

06-17 18:27:27.051: E/AndroidRuntime(23500): FATAL EXCEPTION: main
06-17 18:27:27.051: E/AndroidRuntime(23500): Process: com.mayur.demo, PID: 23500
06-17 18:27:27.051: E/AndroidRuntime(23500): java.lang.RuntimeException: Unable to resume activity {com.mayur.demo/com.mayur.demo.ActivityRegistration}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/286844 (has extras) }} to activity {com.mayur.demo/com.mayur.demo.ActivityRegistration}: java.lang.IllegalArgumentException: width and height must be > 0
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2996)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3025)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3974)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.access$1000(ActivityThread.java:166)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.os.Looper.loop(Looper.java:146)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.main(ActivityThread.java:5511)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at java.lang.reflect.Method.invoke(Method.java:515)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at dalvik.system.NativeStart.main(Native Method)
06-17 18:27:27.051: E/AndroidRuntime(23500): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/286844 (has extras) }} to activity {com.mayur.demo/com.mayur.demo.ActivityRegistration}: java.lang.IllegalArgumentException: width and height must be > 0
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3601)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2983)
06-17 18:27:27.051: E/AndroidRuntime(23500):    ... 13 more
06-17 18:27:27.051: E/AndroidRuntime(23500): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.graphics.Bitmap.createBitmap(Bitmap.java:922)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.graphics.Bitmap.createBitmap(Bitmap.java:901)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.graphics.Bitmap.createBitmap(Bitmap.java:833)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at com.mayur.demo.ActivityRegistration.onActivityResult(ActivityRegistration.java:515)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.Activity.dispatchActivityResult(Activity.java:5514)
06-17 18:27:27.051: E/AndroidRuntime(23500):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3597)
06-17 18:27:27.051: E/AndroidRuntime(23500):    ... 14 more

0 个答案:

没有答案