Android Camera Intent与Android L崩溃

时间:2015-07-06 10:03:43

标签: android android-imageview android-5.0-lollipop

我的应用程序正在适用于所有api直到Kitkat,但它不适用于棒棒糖。我点击我的应用程序中使用相机拍摄照片的那一刻,它崩溃了。

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,kitkat而言非常有效,但不适用于Android L

我得到空指针异常

请建议我一些解决方案

提前致谢

1 个答案:

答案 0 :(得分:1)

如果没有logcat输出,就无法真正说出这一点。由于您在BitmapFacotry类中使用了不推荐使用的方法,它可能会变为空。

在您的活动中全局定义这些

   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");

     }