3

我在我的应用程序中使用相机。我需要拍照并应将其显示在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);

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

4

2 回答 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();


}
于 2013-07-27T05:17:33.003 回答
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);
    }  
}
于 2013-07-27T05:03:23.307 回答