resolveUri因相机上的错误位图uri而失败 [英] resolveUri failed on bad bitmap uri on Camera

查看:209
本文介绍了resolveUri因相机上的错误位图uri而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从相机"和图像选择器"创建位图时遇到麻烦.

I'm having troubles while trying to create a bitmap from a Camera AND an Image Picker.

我使用了一个由相机创建Uri的代码,因此我在已经从图库中加载图片的函数中添加了一个条件. 这是onActivityResult:

I used a code that creates an Uri by the Camera so I added a condition to my function that already load pics from gallery. Here is the onActivityResult :

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

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE || requestCode == RESULT_CAMERA) {
            Uri selectedImage = null;
            if(requestCode == RESULT_LOAD_IMAGE)
            {
                selectedImage = data.getData();
            }
            else if(requestCode == RESULT_CAMERA)
            {
                selectedImage = imageUri;
            }
            if(resultCode == RESULT_OK && null != data) {

                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                imgViewScan.setImageURI(selectedImage);
                try {
                    InputStream stream = getContentResolver().openInputStream(
                            selectedImage);
                    bitmapLoaded = BitmapFactory.decodeStream(stream);
                } catch (IOException e) {
                    Log.e("ScanAc", e.toString());
                }
            }


        }
    }

这是相机的onClick:

and here is the onClick for the Camera :

View.OnClickListener takePicture = new View.OnClickListener() {
    public void onClick(View v) {

        String fileName = "new-photo-name.jpg";
        //create parameters for Intent with filename
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, fileName);
        values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
        //imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
        imageUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        //create new Intent
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        Intent i = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(i, RESULT_CAMERA);
    }
};

我想精确地说,图库图像拾取效果很好,问题只在相机上...

I would like to precise that the gallery image pick works perfectly, the problem is only on the camera...

推荐答案

 private void onClickCamera() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {

        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
        fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        startActivityForResult(takePictureIntent, SELECT_PICTURE_CAMARA);

    } else {
        Toast.makeText(this, getString(R.string.error_no_camera), Toast.LENGTH_LONG).show();
    }

}

在您的takePicture clickListener中尝试此操作

try this in your takePicture clickListener

这篇关于resolveUri因相机上的错误位图uri而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆