Android Camera Intent 因 Android L 而崩溃

Android Camera Intent crashes with Android L

我的应用程序在 Kitkat 之前的所有 api 上都可以正常运行,但在棒棒糖上无法运行。当我在我的应用程序中点击使用相机拍摄照片时,它崩溃了。

cam.setOnClickListener(new OnClickListener() {

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

这是我的 onActivityResult()

    case CAMERA_REQUEST: {

    Uri 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();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(picturePath, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;
        options.inSampleSize = calculateInSampleSize(options,200,100);//512 and 256 whatever you want as scale
        options.inJustDecodeBounds = false;
    Bitmap yourSelectedImage = BitmapFactory.decodeFile(picturePath,options);

    File pngDir = new   File(Environment.getExternalStorageDirectory(),"PicUploadTemp");
    if (!pngDir.exists()) {
        pngDir.mkdirs();
        }

  File pngfile = new File(pngDir,"texture1.jpg");
    FileOutputStream fOut;

    try {
            fOut = new FileOutputStream(pngfile);

             yourSelectedImage.compress(Bitmap.CompressFormat.JPEG, 50,fOut);


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Drawable d = Drawable.createFromPath(pngfile.getPath().toString());
    rlLayout.setBackground(d);

    yourSelectedImage.recycle();
//    resizedBitmap.recycle();

    xfile = pngfile; 
}
break;

我想压缩图像,上面的代码对姜饼、ICS、奇巧非常有效,但对 Android L

无效

我收到空指针异常

请给我一些解决方案

提前致谢

如果没有 logcat 输出,就不能对此说什么,由于您在 BitmapFacotry class.[=14= 中使用了一个已弃用的方法,它可能会变为空]

在您的 activity、

中全局定义这些
   Intent cameraIntent = new Intent(
             android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

   private static final int CAMERA_REQUEST = 1888; 

在按钮中点击像这样调用你的相机意图

    startActivityForResult(cameraIntent, CAMERA_REQUEST);

确保使用

           @Override
           public void onConfigurationChanged(Configuration newConfig) {
             super.onConfigurationChanged(newConfig);
           } 

使用它来检索照片

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

    if (requestCode == CAMERA_REQUEST && resultCode == getActivity().RESULT_OK) {

        //get photo
        Bitmap photo = (Bitmap) data.getExtras().get("data");

     }