【问题标题】:resolveUri failed on bad bitmap uri on CameraresolveUri 在相机上的错误位图 uri 上失败
【发布时间】:2016-09-02 20:12:53
【问题描述】:

我在尝试从相机和图像选择器创建位图时遇到问题。

我使用了一个通过相机创建 Uri 的代码,因此我向我的函数添加了一个条件,该条件已经从图库中加载了图片。 这是 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 :

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

我想确切地说,图库图像选择完美无缺,问题仅出在相机上...

【问题讨论】:

    标签: java android bitmap camera uri


    【解决方案1】:
     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 中试试这个

    【讨论】:

    • 这解决了问题,但我并没有完全按照我的意愿去做。我还想在位图中转换 ImageURI,当我执行 selectedImage = imageUri 时,它会创建一个具有良好价值的 URI,但即使保存图片也不会转换为位图。我想这是因为我在 onActivityResult 中的条件是“如果 resultCode == RESULT_OK”,但我真的不知道
    • 访问这可能是您的问题解决了stackoverflow.com/questions/25828808/…
    【解决方案2】:

    您可以尝试创建如下所示的临时文件。 在 onOnClick 事件中

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    

    对于 onActivityResult

    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), bitmapOptions);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 2014-07-16
      • 1970-01-01
      相关资源
      最近更新 更多