2013-06-21 42 views
3

我有兴趣将图像视图设置为刚刚由相机拍摄的图像。相机应用程序工作得很好,它将SD卡上的图像保存在适当的位置,但是当我尝试加载图像并将其设置为图像视图时,它保持空白。我没有使用最近拍摄的图像的路径,而是尝试对现有图像的路径进行硬编码,但是我得到同样的问题。我检查了其他线程,但在代码中看不到任何差异。将位图设置为ImageView onActivityResult不显示任何内容

这是相机功能:

private void takePicture(){ 
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "/resources/resources/WI"+job_num); 
    image_name = username+"_"+date+".png"; 
    File image_file = new File(imagesFolder, image_name); 
    while(image_file.exists()){ 
     image_name = username+"-"+date+"("+ image_count+").png"; 
     image_count+=1; 
     image_file = new File(imagesFolder,image_name); 
    } 
    image_path = imagesFolder+image_name; 
    Uri uriSavedImage = Uri.fromFile(image_file); 
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    int request_code = 100; 
    startActivityForResult(imageIntent, request_code); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(resultCode == RESULT_OK){ 
     ImageView thumb = (ImageView) findViewById(R.id.thumbnail); 
     Bitmap bmp = BitmapFactory.decodeFile(image_path); 
     thumb.setImageBitmap(bmp); 
     Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show(); 
    } 
    else 
     Toast.makeText(this,"Error Saving Image", Toast.LENGTH_SHORT).show(); 
} 

最后这里是ImageView的:

<ImageView 
    android:id="@+id/thumbnail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/camera" 
    android:layout_marginTop="10dp" 
    android:contentDescription="@string/picture_thumbnail"/> 

什么需要改变?谢谢!

回答

4

试试这个:

final ImageView thumb = (ImageView) findViewById(R.id.thumbnail); 
    thumb.post(new Runnable() { 
     @Override 
     public void run() 
     { 
      Bitmap bmp = BitmapFactory.decodeFile(image_path); 
      thumb.setImageBitmap(bmp); 
      Toast.makeText(this, "Image Saved", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

但我不建议你来解码在UI线程位图。

here

Android相机应用程序编码中作为演员一小位图传送到onActivityResult()的返回意图的照片,下键“数据”。以下代码检索此图像并将其显示在ImageView中。

private void handleSmallCameraPhoto(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    mImageBitmap = (Bitmap) extras.get("data"); 
    mImageView.setImageBitmap(mImageBitmap); 
} 

注:从“数据”这个缩略图可能会为一个图标是好的,但不是很多。处理全尺寸图像需要更多工作。

祝好。

+0

感谢您的回应。不幸的是,我的屏幕上没有任何东西显示,但图像仍然成功保存。 –

+0

你确定位图不是空的吗?请提供您的“image_path”。 –

+0

在这里你可以找到所有你需要采取和保存照片通过意图 - http://developer.android.com/training/camera/photobasics。html –

1

onActivityResult

InputStream stream = null; 
if (bitmap != null) { 
     bitmap.recycle(); 
    } 
    stream = getContentResolver().openInputStream(data.getData()); 
    bitmap = BitmapFactory.decodeStream(stream); 
    stream.close(); 
    thumb.setImageBitmap(bitmap); 

它总是建议循环使用位图试试这个代码。 SO1 & Google Dev

编辑 试试这个代码

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { 
     Bitmap photo = (Bitmap) data.getExtras().get("data"); 
     imageView.setImageBitmap(photo); 
    } 
+0

感谢您的回复。当我执行代码时,它崩溃了:stream = getContentResolver()...此外,出于好奇,为什么重新循环位图很重要? –

+0

我过早地在最后一条评论中“输入”了,对此感到抱歉。 –

+0

@Vayran相应编辑 – MDMalik