0

I have an image sitting in my assets directory here:

 file:///android_asset/content/images/avatar-img.png

I then create a Uri from that and use it to load it into a Drawable:

String imgFile = "file:///android_asset/content/images/avatar-img.png";
myDrawable.setImageUri(Uri.parse(imgFile));

When this runs I see this in the logs and no image:

W/ImageView(24335): Unable to open content: file:///android_asset/content/images/avatar-img.png
W/ImageView(24335): java.io.FileNotFoundException: /android_asset/content/images/avatar-img.png (No such file or directory)
W/ImageView(24335): resolveUri failed on bad bitmap uri: file:///android_asset/content/images/avatar-img.png

However if I feed the same Uri into my WebView the image loads with no issues:

mWebView.loadUrl(imgFile);

So, we know that:

  • The image exist.
  • The image is not corrupted.
  • The image is readable.

Notes:

  • Using BitmapFactory.decodeFile() also fails similarly.
  • I've tried adding READ_EXTERNAL_STORAGE permission, but it didn't work.
  • I don't see any obvious permission issues in the logs.

Test device:

  • Samsung Galaxy S6 Edge
  • Android 7.0
  • APK min 23, target 26

Thoughts?

DataDino
  • 1,507
  • 1
  • 15
  • 30

1 Answers1

0
  file:///android_asset/......

is a notation only understood by WebView.

You should invoke the assets manager and then open an input stream for the file to read its contents.

Code has been posted 365 times.

greenapps
  • 11,154
  • 2
  • 16
  • 19