6

这段代码以前在三星上工作过,但现在我在 Android 2.3.6 上使用 Nexus One,只要我拍照并单击“确定”或从图库中选择一张照片,它就会崩溃。Stacktrace 显示 Uri 上的空指针异常。
我的激活相机的代码如下:

public void activateCamera(View view){      
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // start the image capture Intent
    startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

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

    super.onActivityResult(requestCode, resultCode, data);

    if ((requestCode == CHOOSE_IMAGE_ACTIVITY_REQUEST_CODE  || requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) 
        && resultCode == RESULT_OK && null != data) {

        selectedImage = data.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 picturePath = cursor.getString(columnIndex);

        cursor.close();

        Bitmap bits = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inSampleSize = inSampleSize;

        try {
            bits = BitmapFactory.decodeStream(new FileInputStream(picturePath),null,options);
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }

知道可能是什么问题吗?谢谢!

4

3 回答 3

9

您必须告诉相机,将图片保存在哪里并自己记住 uri:

private Uri mMakePhotoUri;

private File createImageFile() {
    // return a File object for your image.
}

private void makePhoto() {
    try {
        File f = createImageFile();
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mMakePhotoUri = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
        startActivityForResult(i, REQUEST_MAKE_PHOTO);
    } catch (IOException e) {
        Log.e(TAG, "IO error", e);
        Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_MAKE_PHOTO:
            if (resultCode == Activity.RESULT_OK) {
                // do something with mMakePhotoUri
            }
            return;
        default: // do nothing
            super.onActivityResult(requestCode, resultCode, data);
    }
}

您应该使用和保存mMakePhotoUri过度实例状态的值。onCreate()onSaveInstanceState()

于 2013-08-29T03:58:03.370 回答
3

将这个额外的注入到调用 onActivityResult 的 Intent 中,系统将为您完成所有繁重的工作。

File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

这样做可以很容易地将照片检索为位图。

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
}
于 2013-08-29T04:04:06.397 回答
0

不要传递任何额外内容,只需在 onActivityResult 中直接定义您放置或保存文件的路径

 public void openCamera() {

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = createImageFile();
    boolean isDirectoryCreated = file.getParentFile().mkdirs();
    Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);
    if (Build.VERSION.SDK_INT >= 23) {
        tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                "com.scanlibrary.provider", // As defined in Manifest
                file);
    } else {
        tempFileUri = Uri.fromFile(file);
    }

    try
    {
        cameraIntent.putExtra("return-data", true);
        startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
    }
    catch (Exception e)
    {
    }
}

private File createImageFile() {
    clearTempImages();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
            Date());
    File file = new File(ScanConstants.IMAGE_PATH, "IMG_" + timeStamp +
            ".jpg");
    fileUri = Uri.fromFile(file);
    return file;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bitmap = null ;
    if (resultCode == Activity.RESULT_OK ) {
        try {
            if (Build.VERSION.SDK_INT >= 23) {
                    tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                            "com.scanlibrary.provider", // As defined in Manifest
                            file);
                } else {
                    tempFileUri = Uri.fromFile(file);
                }

                    bitmap = getBitmap(tempFileUri);



                    bitmap = getBitmap(data.getData());

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        getActivity().finish();
    }
    if (bitmap != null) {
        postImagePick(bitmap);
    }
}
于 2017-12-01T08:37:20.620 回答