3

I am confused why it is happening because my little knowledge says it should not happen. I have couple of images stored in android asset directory in apk in fact in some directory structure like:

    Assets --> aphotos --> launch50 ---> launch501.jpg

Now for example I want to set an image to an imageview widget pro grammatically like following: -

    Uri uri =    Uri.parse("file:///android_asset/"+context.getString(R.string.photoroot)+"/"+fldr+"/"+introphoto);
    System.out.println("Image URI: "+uri.toString());       
    imageView.setImageURI(uri);

where "fldr" and "introphoto" come from the database. While doing this it generates the following error in "LogCat": -

11-22 19:23:46.689: W/ImageView(12990): Unable to open content: file:///android_asset/aphotos/launch50/launch501.jpg 11-22 19:23:46.689: W/ImageView(12990): java.io.FileNotFoundException: /android_asset/aphotos/launch50/launch501.jpg (No such file or directory) 11-22 19:23:46.689: W/ImageView(12990):   at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)

Can anybody tell that why it's happening because image is there? Thanks in advance!

Fundi
  • 131
  • 2
  • 11

2 Answers2

0

From this question it is obviously not working well to get a URI for an asset file.

I think you need to do it with a stream:

    String path = context.getString(R.string.photoroot)+"/"+fldr+"/"+introphoto;
    InputStream is = getAssets().open(path);
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    imageView.setImageBitmap(bitmap);
    is.close();
Community
  • 1
  • 1
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • The question was why it can't be done without opening the stream which I don't want actually. Image is there, setImageURI method is there then why is not working? – Fundi Nov 23 '12 at 06:09
  • This worked for me:- [link](http://stackoverflow.com/a/8197435/1326579) Thanks for kind replies. – Fundi Nov 23 '12 at 11:54
0

I suggest you to use this code:

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

Glide.with(this).load("file:///android_asset/img/fruit/cherries.jpg").into(imageView);
Elikill58
  • 4,050
  • 24
  • 23
  • 45
Tombstone
  • 208
  • 2
  • 5