使用相机但位图捕获的图像获得空​​白响应

时间:2018-06-18 14:26:45

标签: android image bitmap android-camera android-fileprovider

我想从mCurrentPath字符串中获取位图,该字符串在图库中具有正确的图像存储路径。即使图像在图库中可见,但仍未创建位图

btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (cameraIntent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,  FileProvider.getUriForFile(getApplicationContext(),BuildConfig.APPLICATION_ID + ".provider", photoFile));
                        cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
                    }
                }
            }
        });

创建图像文件

 private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  // prefix
                ".jpg",         // suffix
                storageDir      // directory
        );

        // Save a file: path for use with ACTION_VIEW intents
        mFileName = image.getName();
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        mAbsolutePath = image.getAbsolutePath();
        File file = new File(mAbsolutePath);
        if(file.exists()){
            Log.d("FilePresent","File exist");
        }
        else{
            Log.d("FilePresent","File Doexist");
        }
        Log.d("check path",mCurrentPhotoPath);
        return image;
    }

这是我得到空白位图的地方

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            try {


                File file = new File(mAbsolutePath);
                Uri uri = Uri.fromFile(file);
                //mImageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
                mImageBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
                //--> Here i get blank bitmap <-----------
                //mImageBitmap = decodeFile(mAbsolutePath);
                try {
                    ExifInterface exif = new ExifInterface(mAbsolutePath);
                    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                    Matrix matrix = new Matrix();
                    if (orientation == 6) {
                        matrix.postRotate(90);
                    }
                    else if (orientation == 3) {
                        matrix.postRotate(180);
                    }
                    else if (orientation == 8) {
                        matrix.postRotate(270);
                    }
                    mImageBitmap = Bitmap.createBitmap(mImageBitmap, 0, 0, mImageBitmap.getWidth(), mImageBitmap.getHeight(), matrix, true); // rotating bitmap
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                imgView.setImageBitmap(mImageBitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

同样使用文件提供程序一切都尽可能完美,但是我无法反映的小问题

0 个答案:

没有答案