gpt4 book ai didi

android - 滑动 FileNotFoundException : No content provider when loading images from internet

转载 作者:可可西里 更新时间:2023-11-01 18:47:58 30 4
gpt4 key购买 nike

我制作了自己的 rest api,现在它只有端点可以显示一些图像。这是它在 spring boot 应用程序中的样子

@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 应用程序中所做的

Glide.with(context!!.applicationContext).load("http://myipaddress/api/image/myimage.jpg").into(it)

这就是我从 Glide 中得到的

根本原因(2 个中的 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)

但如果我在浏览器中访问该链接,一切正常。为什么会这样?

最佳答案

如果您的目标是 API 28,并且问题出现在 Android 9 和以 http 开头的 URL 上,则您的问题与明文流量有关如前所述here在网络安全配置中

Starting with Android 9.0 (API level 28), cleartext support is disabled by default.

ensure that all connections to are always done over HTTPS to protect sensitive traffic from hostile networks.

如果您想退出明文流量

仅在您的应用程序 list 中添加此属性

<application
.
android:usesCleartextTraffic="true"
.

>

</application>

如果你想有特定的域有规则

创建文件 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 -

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>

关于android - 滑动 FileNotFoundException : No content provider when loading images from internet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49706014/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com