【问题标题】:returning image from camera intent is giving NullPointerException n Google Maps Api v2从相机意图返回图像在 Google Maps Api v2 中给出空指针异常
【发布时间】:2014-11-26 12:02:15
【问题描述】:

我已经完成了这个编码,一切正常,但是在我拍照后,应用程序崩溃并给出了这个错误:

11-26 13:52:32.791: E/AndroidRuntime(5321): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=null} to activity {com.images.testimages/com.images.testimages.Test}: java.lang.NullPointerException

这是我的相机意图和 OnActivityResult:

public void onMapClick(LatLng point) {

    Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    getApplicationContext().getDir(getResources().getString(R.string.app_name), MODE_PRIVATE);

    Uri imgUri = Uri.fromFile(new File((Environment.getExternalStorageDirectory() + "/" +getResources().getString(R.string.app_name)),new Date().getTime() + ".jpg"));

    getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
    startActivityForResult(getCameraImage, TAKE_PICTURE);
}    
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
    switch(requestCode) { 

        case TAKE_PICTURE:
        if(resultCode == RESULT_OK){  

            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            options.inSampleSize = 8;
            options.inJustDecodeBounds = false;
            MarkerOptions markerOptions = new MarkerOptions().position(point).icon(BitmapDescriptorFactory.fromBitmap(yourSelectedImage));
            googleMap.addMarker(markerOptions);
        }
    }
}}

我现在不知道该怎么办?如果它是内存不足错误,那么我肯定会得到那个错误而不是这个错误吗?

有人可以帮忙吗?

【问题讨论】:

  • 您的错误说明了什么?发布 logcat。

标签: android google-maps android-intent


【解决方案1】:

由于您已经在 Intent 中定义了 EXTRA_OUTPUT 参数并在其中传递了 Uri,因此 onActivityResult() Uri selectedImage = imageReturnedIntent.getData(); 中的这一行将返回 null。

请使用您创建的原始 uri 来获取图像

编辑我的答案:-

 Uri imgUri = Uri.fromFile(new File((Environment.getExternalStorageDirectory() +
     "/" +getResources().getString(R.string.app_name)),new Date().getTime() + ".jpg"));

 getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);

当您在意图中传递此标志时,捕获的图像存储在此 Uri 对象中,所以不要这样做imageReturnedIntent.getData()

只需使用此 Uri 即可获取图像。 Google -> 如何从 Uri 获取图像?

【讨论】:

  • 谢谢,我现在已将其更改为 Uri imgUri = imageReturnedIntent.getData();,但仍然出现相同的错误。
  • getData 返回一个缩略图。传递了额外的输出标志,以便返回完整大小的图像,因此如果您传递了额外的输出,那么 getData 将返回 null,因为您捕获的图像存储在您在意图中传递的额外输出 URI 中。我修改了答案
【解决方案2】:

试试这个

try {
        intent.putExtra("return-data", true);
        startActivityForResult(intent, TAKE_PICTURE);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    相关资源
    最近更新 更多