为什么图片不能显示?

发布于 2022-09-11 21:05:38 字数 1116 浏览 28 评论 0

真的是见鬼了!
为什么图片不能显示啊???

 val mBuilder = AlertDialog.Builder(context)
 val mView = context.layoutInflater.inflate(R.layout.dialog_custom_layout, null)
 mView.view.setImageURI(Uri.parse(item?.image?.url))
 mBuilder.setView(mView)
 Log.d("wdddd",item?.image?.url)
 val mDialog = mBuilder.create()
 mDialog.show()
 

dialog_custom_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/view"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Log

D/wdddd: https://xxx/uploads/work_order_image/image/697/1562829058360.png

Image URL 没错啊,为什么就显示不了?试了Glide也不行!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2022-09-18 21:05:38

提问请不要宣扬封建迷信。我们要破除封建迷信。

PhotoView.setImageURI(Uri) 能直接加载HttpUrl的图片资源吗?先看看源码吧:

public class PhotoView extends AppCompatImageView {
    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
        if (attacher != null) {
            attacher.update();
        }
    }
    
    ... ...
}

PhotoView.setImageURI(Uri)只是简单的重写方法,再跟入AppCompatImageViewImageView源码:

public class AppCompatImageView extends ImageView implements TintableBackgroundView,
        TintableImageSourceView {
    @Override
    public void setImageURI(@Nullable Uri uri) {
        super.setImageURI(uri);
        if (mImageHelper != null) {
            mImageHelper.applySupportImageTint();
        }
    }
    
    ... ...
}
public class ImageView extends View {
    public void setImageURI(@Nullable Uri uri) {
        if (mResource != 0 || (mUri != uri && (uri == null || mUri == null || !uri.equals(mUri)))) {
            updateDrawable(null);
            mResource = 0;
            mUri = uri;

            final int oldWidth = mDrawableWidth;
            final int oldHeight = mDrawableHeight;

            resolveUri();

            if (oldWidth != mDrawableWidth || oldHeight != mDrawableHeight) {
                requestLayout();
            }
            invalidate();
        }
    }
    
    private void resolveUri() {
        if (mDrawable != null) {
            return;
        }

        if (getResources() == null) {
            return;
        }

        Drawable d = null;

        if (mResource != 0) {
            try {
                d = mContext.getDrawable(mResource);
            } catch (Exception e) {
                Log.w(LOG_TAG, "Unable to find resource: " + mResource, e);
                // Don't try again.
                mResource = 0;
            }
        } else if (mUri != null) {
            d = getDrawableFromUri(mUri);

            if (d == null) {
                Log.w(LOG_TAG, "resolveUri failed on bad bitmap uri: " + mUri);
                // Don't try again.
                mUri = null;
            }
        } else {
            return;
        }

        updateDrawable(d);
    }

    private Drawable getDrawableFromUri(Uri uri) {
        final String scheme = uri.getScheme();
        if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            try {
                // Load drawable through Resources, to get the source density information
                ContentResolver.OpenResourceIdResult r =
                        mContext.getContentResolver().getResourceId(uri);
                return r.r.getDrawable(r.id, mContext.getTheme());
            } catch (Exception e) {
                Log.w(LOG_TAG, "Unable to open content: " + uri, e);
            }
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
                || ContentResolver.SCHEME_FILE.equals(scheme)) {
            try {
                Resources res = sCompatUseCorrectStreamDensity ? getResources() : null;
                ImageDecoder.Source src = ImageDecoder.createSource(mContext.getContentResolver(),
                        uri, res);
                return ImageDecoder.decodeDrawable(src, (decoder, info, s) -> {
                    decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
                });
            } catch (IOException e) {
                Log.w(LOG_TAG, "Unable to open content: " + uri, e);
            }
        } else {
            return Drawable.createFromPath(uri.toString());
        }
        return null;
    }
    ... ...
}

从源码看来,ImageView.setImageURI(Uri)是没有直接加载HttpUrl图片资源的能力的。正确的做法如下:

Glide.with(this).load("https://xxx/uploads/work_order_image/image/697/1562829058360.png").into(imageView);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文