android - 将位图设置为 ImageView onActivityResult 不显示任何内容

标签 android camera imageview

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

这是相机功能:

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

需要改变什么?谢谢!

最佳答案

试试这个:

    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 :

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data". The following code retrieves this image and displays it in an ImageView.

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

Note: This thumbnail image from "data" might be good for an icon, but not a lot more. Dealing with a full-sized image takes a bit more work.

最良好的祝愿。

关于android - 将位图设置为 ImageView onActivityResult 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243538/

相关文章:

android - 如何将应用程序中的图像从图库添加到 android 中的 recyclerview?

android - 在android中的surfaceview上创建cameraview(掩码)

java - 如何在 imageView 数组中的每个 imageView 周围绘制一个矩形?

java - 使用 Contacts.Groups._COUNT 获取联系人组时列_count 无效?

android - proguard不保留注释

android - arm-gcc编译器编译的c代码的头文件

android - Bug 录制视频 android

android - 使用 Ionic 创建自定义相机 View

iphone - 跨 View Controller 传输图像

java - android中Onclicklistener的空指针异常