Glide FileNotFoundException: No content provider w

2019-04-04 07:10发布

问题:

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);
    }

and this is storage service method

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

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

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?

回答1:

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

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.

If you want to Opt out of cleartext traffic

Add this property on your application manifests only

<application
 . 
 android:usesCleartextTraffic="true"
 .

 >

 </application>

And if you want to have specific domains to have the rule

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 -

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


回答2:

Add to gradle annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'