Android 将位图设置为ActivityResult上的图像视图不是';我什么也没展示

Android 将位图设置为ActivityResult上的图像视图不是';我什么也没展示,android,camera,imageview,Android,Camera,Imageview,我感兴趣的是将图像视图设置为相机刚刚拍摄的照片。摄像头应用程序工作正常,它将图像保存在sd卡上的适当位置,但当我尝试加载图像并将其设置为图像视图时,它将保持空白。我没有使用最近拍摄的图像的路径,而是尝试对现有图像的路径进行硬编码,但我遇到了同样的问题。我已经检查了其他线程,但在代码中看不到任何差异 这是相机的功能: private void takePicture(){ Intent imageIntent = new Intent(android.provider.MediaStore

我感兴趣的是将图像视图设置为相机刚刚拍摄的照片。摄像头应用程序工作正常,它将图像保存在sd卡上的适当位置,但当我尝试加载图像并将其设置为图像视图时,它将保持空白。我没有使用最近拍摄的图像的路径,而是尝试对现有图像的路径进行硬编码,但我遇到了同样的问题。我已经检查了其他线程,但在代码中看不到任何差异

这是相机的功能:

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"/>


需要改变什么?谢谢

onActivityResult

  InputStream stream = null;
 if (bitmap != null) {
      bitmap.recycle();
    }
    stream = getContentResolver().openInputStream(data.getData());
    bitmap = BitmapFactory.decodeStream(stream);
    stream.close();
    thumb.setImageBitmap(bitmap);
始终建议回收位图

编辑 试试这个代码

 if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
试试这个:

    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线程上解码位图

发件人:

Android摄像头应用程序将发送给onActivityResult()的返回意图中的照片编码为extras中“data”键下的小位图。以下代码检索此图像并将其显示在ImageView中

注意:这张来自“数据”的缩略图可能适合用作图标,但仅此而已。处理全尺寸图像需要更多的工作


最好的祝愿。

感谢您的回复。当我实现代码时,它在以下行崩溃:stream=getContentResolver()。。。另外,出于好奇,为什么回收位图很重要?我在最后一条评论中过早地点击了“回车”,对此表示抱歉。@Vayran进行了相应的编辑。它仍然会崩溃,但在eclipse调试期间,我注意到在调用openInputStream时用作参数的数据为空。感谢您的回复。不幸的是,我的屏幕上没有显示任何内容,但图像仍然成功保存。您确定位图不是空的吗?请提供您的“图像路径”。在这里,您可以找到通过intent拍摄和保存照片所需的所有内容-图像路径在第一个函数“takePicture”中定义,并且在您指出之前定义不正确,谢谢。即使路径正确,也不会显示任何内容。它应该是image\u path=Environment.getExternalStorageDirectory()+“/resources/resources/WI1/”+image\u name。把它擦掉,它现在可以工作了。谢谢。更新答案,试着得到你的图像,看看Android开发者指南。
private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
}