Glide FileNotFoundException:从Internet加载图像时没有内容提供者

时间:2018-04-07 09:46:53

标签: android rest android-glide

我做了自己的休息api,现在它只有端点,我显示了一些图像。这是它在春季启动应用程序中的外观

 public byte[] loadFileAsByteArray(String filename) {
            Resource resource = loadFile(filename);
            try {
                return IOUtils.toByteArray(resource.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

这是存储服务方法

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

在这里我在我的Android应用程序中做了什么

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)

所以我从滑翔中得到了什么

根本原因(2/2)

import os
import subprocess
import time
from subprocess import Popen,PIPE
import pexpect
process = subprocess.Popen('sudo gnome-terminal  -x ./client.sh', stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
process.communicate(input = "1") 

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

3 个答案:

答案 0 :(得分:4)

如果您将API 28定位为目标,并且该问题出现在Android 9上,并且以http开头的网址是明文流量 如网络安全配置中的here

  

从Android 9.0(API级别28)开始,默认情况下禁用明文支持。

     

确保所有连接均始终通过HTTPS进行,以保护   来自敌对网络的敏感流量。

如果您想退出明文流量

仅将此属性添加到您的应用程序清单中

<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>

答案 1 :(得分:0)

就我而言,我只是将链接放在字符串变量内

selectToday() {this.model = this.calendar.getToday();}

问题解决!

答案 2 :(得分:-1)

添加到gradle annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'