滑行FileNotFoundException:从Internet加载图像时没有内容提供者 [英] Glide FileNotFoundException: No content provider when loading images from internet
问题描述
我制作了自己的rest api,现在它只有显示一些图像的端点.这里是它在Spring Boot应用程序中的外观
I made my own rest api and now it just has endpoint where I show some images. Here how it looks in spring boot application
@GetMapping("/image/{name:.+}")
public byte[] getImage(@PathVariable(value = "name") String name) {
return storageService.loadFileAsByteArray(name);
}
这是存储服务方法
public byte[] loadFileAsByteArray(String filename) {
Resource resource = loadFile(filename);
try {
return IOUtils.toByteArray(resource.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这里是我在Android应用程序中所做的事情
and here what I do in my android app
Glide.with(context!!.applicationContext).load("http://myipaddress/api/image/myimage.jpg").into(it)
这就是我从滑行中得到的东西
So here what I get from glide
根本原因(第2个,共2个)
Root cause (2 of 2)
java.io.FileNotFoundException: No content provider: http://myipaddress/api/image/myimage.jpg
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1112)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:964)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:887)
at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:22)
at com.bumptech.glide.load.data.AssetFileDescriptorLocalUriFetcher.loadResource(AssetFileDescriptorLocalUriFetcher.java:13)
at com.bumptech.glide.load.data.LocalUriFetcher.loadData(LocalUriFetcher.java:44)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)
at com.bumptech.glide.load.engine.DecodeJob.onDataFetcherFailed(DecodeJob.java:394)
at com.bumptech.glide.load.engine.SourceGenerator.onLoadFailed(SourceGenerator.java:119)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:151)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:142)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:97)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.startNextOrFail(MultiModelLoader.java:148)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.onLoadFailed(MultiModelLoader.java:142)
at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:65)
at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:97)
at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:62)
at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:299)
at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:269)
at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:446)
但是,如果我在浏览器中转到该链接,则一切正常.为什么会这样?
but if i go to that link in my browser everything just works. Why this is happening?
推荐答案
如果您以API 28为目标,并且该问题出现在Android 9上,并且以http
开头的URL出现明文流量
如此处在网络安全配置
If you are targeting API 28 on and the issue appears on Android 9 and the URL starting with http
your issue is with cleartext traffic
as mentioned here in Network security configuration
从Android 9.0(API级别28)开始,默认情况下禁用明文支持.
Starting with Android 9.0 (API level 28), cleartext support is disabled by default.
确保所有连接均始终通过HTTPS进行,以保护 来自敌对网络的敏感流量.
ensure that all connections to are always done over HTTPS to protect sensitive traffic from hostile networks.
如果您想退出明文流量
仅将此属性添加到您的应用程序清单中
<application
.
android:usesCleartextTraffic="true"
.
>
</application>
如果您要让特定域具有规则
And if you want to have specific domains to have the rule
创建文件res/xml/network_security_config.xml
Create file res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">secure.example.com</domain>
</domain-config>
</network-security-config>
AndroidManifest.xml-
AndroidManifest.xml -
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
这篇关于滑行FileNotFoundException:从Internet加载图像时没有内容提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!