Android 如何将位图设置为imageView?

Android 如何将位图设置为imageView?,android,bitmap,imageview,Android,Bitmap,Imageview,我一直在努力将图像(使用uri获取)设置到ImageView中 我在做什么? class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { private final WeakReference<Attachment> imageViewReference; public BitmapWorkerTask(Attachment imageView) { // Use a

我一直在努力将图像(使用uri获取)设置到
ImageView

我在做什么?

class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
    private final WeakReference<Attachment> imageViewReference;

    public BitmapWorkerTask(Attachment imageView) {
        // Use a WeakReference to ensure the ImageView can be garbage collected
        imageViewReference = new WeakReference<Attachment>(imageView);
    }

    // Decode image in background.
    @Override
    protected Bitmap doInBackground(Integer... params) {
        return ImageResizer.decodeSampledBitmapFromFile(imageViewReference.get().getPath(), 200, 100);
    }

    // Once complete, see if ImageView is still around and set bitmap.
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        if (imageViewReference != null && bitmap != null) {
            final Attachment imageView = imageViewReference.get();
            if (imageView != null) {
                profilePic.setImageBitmap(bitmap);
            }
            else {
                Log.v("BLAH[Inner]", (imageViewReference ==null) +""+(bitmap == null));
            }
        }
        else {
            Log.v("BLAH", (imageViewReference ==null) +""+(bitmap == null));
        }
    }
}
类BitmapWorkerTask扩展了AsyncTask{
私人最终参考

我使用层次查看器检查布局


我希望您在清单中添加了权限:

<uses-permission android:name="android.permission.INTERNET" />


有时我们会忽略一些小事情。

您使用的是始终有效的setImageBitmap,但您可以使用以下代码片段

BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
imageView.setBackgroundDrawable(ob);
改变

profilePic.setImageBitmap(bitmap)


从您提供的信息来看,可能是文件有问题,或者是
ImageResizer。decodeSampledBitmapFromFile
未工作,返回了一个空的或透明的位图

您可以在
onPostExecute
中添加此代码并发布结果吗

int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.v("BLAH", "width : " + width);
Log.v("BLAH", "height : " + height);
if(width > 0 && height > 0) {
    Log.v("BLAH", "pixel : " + bitmap.getPixel(width/2,height/2));
}

很抱歉问这个问题,您确认异步任务已经启动了吗?哦..是的,我执行了…:-)也许您应该在
if(imageView!=null)中添加一个日志{
还可以查看它何时被调用done…它将在if中出现。此外,您可能需要先添加一个默认的android:src以查看它是否正确显示我正在获取的图像是本地的(在手机中)uri。如果尚未添加,请尝试检查读写权限,因为您的代码看起来是正确的。另一种方法是:在清单中执行此操作。我确信我使用的是《开发人员指南》中的此代码段……是否正确……似乎是这样。请检查我的更新,看看是否可以尝试添加此日志宽度:12高度:9像素:-7240337它这是一个非常小的图像,12 x 9像素。实际上,我想我可以在你上传的图像上找到它,它是否设置为水平居中并对齐顶部?可能是profilePic的参考不正确,这就是它不显示的原因。在这里添加完整代码,以便我们实现解决方案。
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.v("BLAH", "width : " + width);
Log.v("BLAH", "height : " + height);
if(width > 0 && height > 0) {
    Log.v("BLAH", "pixel : " + bitmap.getPixel(width/2,height/2));
}