ImageView无法显示图像。

3

我在我的应用程序中使用相机。我需要拍照并将其显示在imageview中。我正在使用以下代码从相机中拍照并显示它。

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
    startActivityForResult(intent, 0);
    imageView.setImageURI(capturedImageUri);

这仅适用于两个或有时三个图像,然后 imageview 不显示图像,但是图像已经正确地 存储在SD卡中。另外我也使用了


   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
   Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
   Imageview.setImageBitmap(bitmap);

但是我面临着相同的问题。请有人能帮帮我吗?

在 logcat 中是否显示任何错误? - JoxTraex
你有收到任何错误吗?看起来你正在尝试显示相机拍摄的全尺寸图像,这将消耗大量内存。 - Kai
不,它没有显示任何错误。我也检查了文件大小。它几乎与显示的图像大小相同。我还尝试过压缩,但仍然面临同样的问题。 - Mathan
2个回答

1
capturedImageUri  will return path to captured Image not the actual Image..

此外,重要提示-- 如果您不需要完整大小的图像,请使用-
   // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);   Comment this line
    Bitmap image = (Bitmap) data.getExtras().get("data");

使用以下代码获取完整尺寸的位图 -
  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                try
                {
                    // place where to store camera taken picture
                    tempPhoto = createTemporaryFile("picture", ".png");
                    tempPhoto.delete();
                }
                catch(Exception e)
                {

                    return ;
                }
                mImageUri = Uri.fromFile(tempPhoto);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                startActivityForResult(cameraIntent, 1);

private File createTemporaryFile(String part, String ext) throws Exception
    {
           // File to store image temperarily.
        File tempDir= Environment.getExternalStorageDirectory();
        tempDir=new File(tempDir.getAbsolutePath()+"/.temp/");
        if(!tempDir.exists())
        {
            tempDir.mkdir();
        }
        return File.createTempFile(part, ext, tempDir);
    }




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

            ContentResolver cr = getApplicationContext().getContentResolver();
            try {
                cr.notifyChange(mImageUri, null);
                File imageFile = new File(tempPhoto.getAbsolutePath());


            } catch (Exception e) {
                e.printStackTrace();
            }

            Bitmap photo=null;
            if (resultCode == 1) {
                try {
                    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(tempPhoto));
imageView.setImageBitmap(photo);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                } catch (Exception e) {

                    e.printStackTrace();


}

嗨,Amol,谢谢你的回复。但我仍然面临同样的问题。 - Mathan
1
@Mathan 你需要全尺寸的图像吗?还是缩略图就可以了? 因为添加 intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); 将返回全尺寸的图像。 - Mr.India

0

你尝试过将setImageUri逻辑放在活动回调onActivityResult中吗?

我认为你正在尝试在任何内容保存到该URI之前向ImageView提供图像。您需要连接到在相机实际拍摄照片时触发的回调(即相机活动完成并报告其结果)

这个答案有一个完整的写作

https://dev59.com/kW025IYBdhLWcg3wc1xM#5991757

可能是类似于

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
}

嗨Selecsosi,谢谢你的回复。我已经使用了你的建议,但问题仍然存在。 - Mathan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接