在Android中捕获图像会在onActivityResult上出现错误

时间:2016-07-19 12:37:42

标签: java android

我试图在Android中捕获图像,但我收到的错误如下:

  

致命的例外:主要                                                                    处理:com.pace.planes,PID:18741                                                                    java.lang.RuntimeException:将结果ResultInfo {who = null,request = 1888,result = -1,data = Intent {dat = content:// media / external / images / media / 4532}}传递给activity {com。 pace.planes / com.pace.planes.Confirmation}:java.lang.NullPointerException

虽然我使用了所有建议的解决方案而不是Stack Overflow。这是我的代码:

captureImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
    });

这里是onActivityResult代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode != RESULT_CANCELED && data != null){

        if (requestCode == CAMERA_REQUEST && resultCode== Activity.RESULT_OK) {
            Bitmap imagebitmap = (Bitmap) data.getExtras().get("data");
            Uri tempUri = getImageUri(getApplicationContext(), imagebitmap);
            // CALL THIS METHOD TO GET THE ACTUAL PATH
            finalFilePath = new File(getRealPathFromURI(tempUri));
            // imageBitmap = Compressor.getDefault(this).compressToBitmap(finalFilePath);
            captureImage.setScaleType(ImageView.ScaleType.FIT_XY);
            imagebitmap = new Compressor.Builder(this)
                .setQuality(10)
                .setCompressFormat(Bitmap.CompressFormat.PNG)
                .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath())
                .build()
                .compressToBitmap(finalFilePath);
            captureImage.setImageBitmap(imagebitmap);
            //set status image is seleted
            IsImageSelected = true;
        }
    }
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

0 个答案:

没有答案