1

I have issue to retrieve image path from assets folder.

I have an image folder in assets folder. Within the folder i have three different folders.

Here is the code that i used:

String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ; try{ AssetManager mngr =getAssets(); InputStream BulletImgInput = mngr.open(IntroImage1); //InputStream BulletImgInput = mngr.open("image/Malay/bullet.png"); Bitmap bitmapBullet = BitmapFactory.decodeStream(BulletImgInput); BulletImage.setImageBitmap(bitmapBullet); }catch(final IOException e){ e.printStackTrace(); } 

I am wondering why can't i display the image? Because I have try to retrieve it through this code:

InputStream BulletImgInput = mngr.open("image/Malay/bullet.png"); 

It did retrieve the file, but with the string that i replaced in mngr.open it doesn't show up.

Really need you guys to help up.Thanks.

0

5 Answers 5

7

You don't need the AssetManager. You can do

BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")

Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.

Update: Explanation The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/image/Malay/bullet.jpg.

Sign up to request clarification or add additional context in comments.

2 Comments

Ya i have certain reason to use assets file. Can you briefly tell me how the code you provide works? Sorry i m super new to android. THanks
This does not work for me. I get a file not found exception. Even though i use BitmapFactory.decodeFile("file:///android_asset/anims/x.gif", options); the logcat error shows it as FileNotFoundException: /file:/android_asset/anims/x.gif. Any idea what the problem is?
4

Try this:

try { // get input stream InputStream ims = getAssets().open("avatar.jpg"); // load image as Drawable Drawable d = Drawable.createFromStream(ims, null); // set image to ImageView mImage.setImageDrawable(d); } catch(IOException ex) { Log.e("I/O ERROR","Failed when ..." } 

your BulletImage

Comments

3
String url = "file:///android_asset/NewFile.txt"; String url = "file:///android_asset/logo.png"; 

you can access any file....

3 Comments

but it is not a txt file. It is a .png file. So any other suggestion?
@YuenTong you can also access png files dear
It doesnt work for me. TT When i use this code: InputStream BulletImgInput = mngr.open("image/Malay/bullet.png"); , It works. but not to replace the url by the string assign by different variable. I already try to debug it by displaying the String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ; and i got the same path as this "image/Malay/bullet.png"
1
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png"); 

Maybe this could work for u.

1 Comment

It doesnt work for me. TT When i use this code: InputStream BulletImgInput = mngr.open("image/Malay/bullet.png"); , It works. but not to replace the url by the string assign by different variable. I already try to debug it by displaying the String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ; and i got the same path as this "image/Malay/bullet.png"
0

May be problem is in missed image part of path?

String IntroImage1= "image/" + languageSelected + "/" + Intro1ImagePath + ".png" ; 

instead of

String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ; 

Upd: Check values of languageSelected and Intro1ImagePath also.

4 Comments

this is my directory: assets --> image ---> (3 different folder with the image inside). So i think including the ""image/" is needed right?
@YuenTong Yes, including of "image/" is needed.
It doesnt work for me. TT When i use this code: InputStream BulletImgInput = mngr.open("image/Malay/bullet.png"); , It works. but not to replace the url by the string assign by different variable. I already try to debug it by displaying the String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ; and i got the same path as this "image/Malay/bullet.png"
@YuenTong I see in your code that you use ImagePath variable instead of IntroImage1. What is value of ImagePath?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.