2013-07-27 54 views
3

我在我的应用程序中使用相机。我需要拍照并应在imageview中显示它。我正在使用以下代码从相机拍摄照片并进行显示。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); 

但我面临同样的问题。任何人都可以帮助我。

+0

是否有任何错误显示logcat的吗? – JoxTraex

+0

你有什么错误吗?它看起来像你试图显示相机拍摄的全尺寸图像,这将消耗过多的内存。 – Kai

+0

不,它不显示任何错误。我也检查了文件大小。它与显示图像几乎相同。我也尝试过压缩。我仍然面临同样的问题。 – Mathan

回答

0

您是否尝试过把你的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); 
    } 
} 
+0

Hi Selecsosi,谢谢你的回应。我已经使用你的建议。问题仍然存在。 – Mathan

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

而且,重要Note--如果你不需要使用 -

全尺寸的图象
// 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(); 


} 
+0

Hi Amol感谢您的回复。但我仍面临同样的问题。 – Mathan

+1

@Mathan你需要全尺寸的图片吗?或者您可以使用缩略图吗? 因为添加'intent.putExtra(MediaStore.EXTRA_OUTPUT,capturedImageUri);'将返回完整大小的图像。 –

+0

见编辑的代码。 –