android:从Camera捕获图像得到一个null

时间:2016-04-01 07:34:16

标签: android camera

我正在获得" photoFile"在onActivityResult()

中为null

在捕获按钮上单击我调用方法dispatchTakePictureIntent() 但是当我返回时它会变为空。

当图像分辨率/尺寸非常大,如4012 * 4012等时会出现此问题,特别是在Android版本6.0 +上发生

这是我的代码

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = Util.createImageFile();
            mCurrentPhotoPath = photoFile.getAbsolutePath();

            System.out.println( "photoFile:" + photoFile.getAbsolutePath());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {

            System.out.println("@@@@ photoFile: " + photoFile.getAbsolutePath());

            /*takePictureIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,1024);
            takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    mCurrentPhotoPath);*/

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));

            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);

        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_CANCELED) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            try {
                //Bundle extras = data.getExtras();
                // Log.i(TAG, "extras: " + extras.get("data"));

                Bitmap mphoto = (Bitmap) data.getExtras().get("data");


                System.out.println( "***photoFile: " + data.getExtras().get("data"));

            } catch (Exception e) {
                // Log.i(TAG, "Exception:" + e.getMessage());
            }

            ShowPopupDialog(mCurrentPhotoPath);
        }

    }
}

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

将您的OnActivityResult代码更改为:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode != RESULT_CANCELED) {
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                try {    

                      File fil = new File(Environment.getExternalStorageDirectory().toString());
                            for (File temp : fil.listFiles()) {
                                if (temp.getName().equals("temp.jpg")) {
                                    fil = temp;
                                    break;
                                }
                            }
                            Bitmap bitmap = null;

                            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                            Bitmap bmp = BitmapFactory.decodeFile(fil.getAbsolutePath(),bitmapOptions);
    //
                            Log.e("edit", "new image path is " + fil.getAbsolutePath());
                 } catch (Exception e) {
                    // Log.i(TAG, "Exception:" + e.getMessage());
                }

                ShowPopupDialog(mCurrentPhotoPath);
            }

        }