这几天,碰到一个诡异的问题,发现原来Android上好好的图片,无法显示了。
经过代码比对,也一直找不到问题。
没办法,只好调试代码吧,发现让图片显示的程序为:
Glide.with(mContext).load(FileDataUrl + TaskImage + mLst.get(position).getImg()).thumbnail(0.1f).into(myHolder.mimage);
于是,怀疑这部分代码是不是有问题。
查询相关说明:
Glide的使用说明如下:
Glide is an Image Loader Library for Android developed by bumptech and is a library that is recommended by Google. It has been used in many Google open source projects including Google I/O 2014 official application. It provides animated GIF support and handles image loading/caching.
翻译意思就是:Glide是Goolge推荐的图片处理函数。他提供了GIF支持,并可以 *** 作图片的加载和缓存。
基本用法如下:
Glide.with(context)
.load("http://www.kaotop.com/file/tupian/20220518/300.png")
.override(300, 200)
.into(ivImg);
既然是出错了,那怎么判断出错了。
但Glide提供了一个说明:如果出错,不会直接报错;要想查看错误,必须配置错误监听函数。
如下:
Glide
.with(mContext)
.load(Uri.parse(url))
.error(R.drawable.ic_photo)
.thumbnail(0.1f).
listener(new RequestListener() {
// 错误处理
@Override
public boolean onException(Exception e, Uri model, Target target, boolean isFirstResource) {
e.printStackTrace();
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Uri model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
}
).
into(myHolder.mimage);
其中,listner对应的为错误处理函数。
*** 作以后,果然在listner对应的onException函数里得到了错误消息,告诉网址无法访问。
如下:
W/System.err: java.lang.Exception: Failed to load model: '相关地址'
W/System.err: at com.bumptech.glide.request.GenericRequest.onSizeReady(GenericRequest.java:441)
W/System.err: at com.bumptech.glide.request.target.ViewTarget$SizeDeterminer.getSize(ViewTarget.java:211)
W/System.err: at com.bumptech.glide.request.target.ViewTarget.getSize(ViewTarget.java:100)
W/System.err: at com.bumptech.glide.request.GenericRequest.begin(GenericRequest.java:272)
W/System.err: at com.bumptech.glide.request.ThumbnailRequestCoordinator.begin(ThumbnailRequestCoordinator.java:90)
W/System.err: at com.bumptech.glide.manager.RequestTracker.resumeRequests(RequestTracker.java:83)
W/System.err: at com.bumptech.glide.RequestManager.resumeRequests(RequestManager.java:180)
W/System.err: at com.bumptech.glide.RequestManager.onStart(RequestManager.java:203)
W/System.err: at com.bumptech.glide.manager.ActivityFragmentLifecycle.onStart(ActivityFragmentLifecycle.java:50)
W/System.err: at com.bumptech.glide.manager.SupportRequestManagerFragment.onStart(SupportRequestManagerFragment.java:135)
W/System.err: at android.support.v4.app.Fragment.performStart(Fragment.java:2265)
W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1369)
W/System.err: at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
W/System.err: at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:2966)
W/System.err: at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:212)
W/System.err: at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
W/System.err: at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1446)
W/System.err: at android.app.Activity.performStart(Activity.java:7990)
W/System.err: at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3535)
W/System.err: at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:237)
W/System.err: at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:217)
W/System.err: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:189)
W/System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:113)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2213)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:107)
W/System.err: at android.os.Looper.loop(Looper.java:238)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7864)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)
但比较奇怪的是:我自己把相关网址进行粘贴到IE或Chrome等浏览器里,把图片下载了下来。
大为困惑,满脸黑人问号???
于是,继续查资料:
查了好多资料后,终于在StackOverflow(https://stackoverflow.com/questions/55473076/class-com-bumptech-glide-load-engine-glideexception-failed-to-load-resource)上查询到这句话:
”For me the problem was with Http...
or HTTP...
. I changed it to http...
(all lower letters) and it worked.
Both Glide
and Picasso
had the same problem with it.“
也就是说:Glide和Picasso这种图片处理工具,都是只支持http开头的链接,不支持开头大写。
仔细查代码,发现本人在代码重构中,把http变成了首字母大写(Refactor的锅)。修改字符串为http后,发现图片能查看,问题解决。
PS:God or deveil is in the detail。只有真正了解一段代码,才可以完全运用这段代码。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)