将位图设置为ImageView onActivityResult不显示任何内容 [英] Setting bitmap to ImageView onActivityResult isn't displaying anything

查看:124
本文介绍了将位图设置为ImageView onActivityResult不显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am interested in setting an image view to a picture just taken by the camera. The camera application works just fine and it saves the image on the sd card in the appropriate location, but when I try to load the image and set it to the image view, it stays blank. Instead of using the path of the image recently taken, I also tried hard coding the path of an existing image but I get the same issue. I have checked other threads but I can't see any differences in my code.

这是相机功能:

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:

Finally here is the 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线程上解码位图。

But i don't recommend you to decode bitmap on UI thread.

来自此处


Android Camera应用程序将返回的Intent中的照片编码为onActivityResult(),作为附加内容中的一个小位图,位于关键字data下。以下代码检索此图像并将其显示在ImageView中。

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.

祝福。

这篇关于将位图设置为ImageView onActivityResult不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆