无法从文件URI

时间:2017-12-30 10:55:44

标签: android

我一直在尝试用相机拍照,并把这张照片放在我的ImageView中。首先我发现过去的版本24你需要使用FileProvider,这似乎现在可以工作。但是,每当我尝试从URI创建BitMap时,它仍然是null。我想知道路径应该是什么,为了从这个创建一个位图?

private File createImageFile() throws IOException {
    File storageDir = Environment.getExternalStorageDirectory();
    File image = File.createTempFile(
            "submit",
            ".jpg",
            storageDir
    );
    return image;
}

private void dispatchCameraIntent() throws Exception {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();

                Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.example.daan.tvshowtracker.provider", photoFile);
                // Bitmap bitmap = BitmapFactory.decodeFile(path.toString());       <- also tried it this way
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), path);
                submitImage.setImageBitmap(bitmap);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }

            ......
        }
    }

上面的代码是我用来创建图像和调度摄像头的功能。我在我的Manifest中有提供程序设置,以及我的res文件夹中的provider_paths.xml文件,如不同的帖子中所指定的。我得到的数据是:

path: "content://com.example.daan.tvshowtracker.provider/external_files/submit111012311.jpg"

photofile: "/storage/emulated/0/submit111012311.jpg"

任何人都知道为什么位图仍然是null?任何帮助表示赞赏!

编辑:已更新为代码 我创建了onActivityResult(),它返回intent中的数据。在putExtra中,我放置了我的图像文件。

private void dispatchCameraIntent() throws Exception {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        try {
            photoFile = createImageFile();

            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoFile);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
                Uri path = FileProvider.getUriForFile(getApplicationContext(), "com.example.daan.tvshowtracker.provider", photoFile);
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                submitImage.setImageBitmap(photo);
        }
    }

但是,当我从意图中获取此数据时,它只显示一个小的黑色图像?虽然Only displays this as image, instead of the image it took

,但数据似乎存在

2 个答案:

答案 0 :(得分:1)

您尝试在拍照前从媒体商店获取位图。

在致电startActivityForResult()之前,您无法做到这一点。

在你打电话之后也没有。

你只能在拍完照片后才能这样做。

执行onActivityResult()

答案 1 :(得分:0)

Try this code:
Bitmap bitmap = getBitmapFromView(idForSaveView);
            try {
                File file = new File(this.getExternalCacheDir(), "appdiam.png");
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                fOut.flush();
                fOut.close();
                file.setReadable(true, false);
                final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                intent.setType("image/png");
                startActivity(Intent.createChooser(intent, "Share image via"));
            } catch (Exception e) {
                e.printStackTrace();
            }


      private Bitmap getBitmapFromView(View view) {
            Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(returnedBitmap);
            Drawable bgDrawable = view.getBackground();
            if (bgDrawable != null) {
                //has background drawable, then draw it on the canvas
                bgDrawable.draw(canvas);
            } else {
                //does not have background drawable, then draw white background on the canvas
                canvas.drawColor(Color.WHITE);
            }
            view.draw(canvas);
            return returnedBitmap;
        }