【问题标题】:ImageView not displaying ImageImageView 不显示图像
【发布时间】:2013-07-27 14:33:57
【问题描述】:

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

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

这仅适用于两个或有时三个图像,然后imageview 不显示图像,但图像是正确的stored in SD card。 或者我也使用过

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

但我也面临同样的问题。谁能帮帮我。

【问题讨论】:

  • logcat 中是否出现任何错误?
  • 您有任何错误吗?您似乎正在尝试显示相机拍摄的全尺寸图像,这会消耗过多的内存。
  • 没有。它不显示任何错误。我也检查了文件大小。它与显示图像几乎相同。我也试过压缩。我仍然面临同样的问题。

标签: android camera imageview


【解决方案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 你需要全尺寸图片吗?或者你对缩略图没意见?因为添加intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); 将返回全尺寸图像。
【解决方案2】:

您是否尝试过将 setImageUri 逻辑放入活动回调 onActivityResult

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

这个答案有完整的记录

https://stackoverflow.com/a/5991757/418505

可能是这样的

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,感谢您的回复。我已经使用了你的建议。问题仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多