Yuen Tong
Yuen Tong

Reputation: 869

Retrieve image path from assets folder

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.

Upvotes: 1

Views: 30744

Answers (5)

K_Anas
K_Anas

Reputation: 31466

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

Upvotes: 4

Vikram Bodicherla
Vikram Bodicherla

Reputation: 7123

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.

Upvotes: 7

Sergey Glotov
Sergey Glotov

Reputation: 20336

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.

Upvotes: 0

MAC
MAC

Reputation: 15847

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

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

you can access any file....

Upvotes: 3

Thkru
Thkru

Reputation: 4199

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

Maybe this could work for u.

Upvotes: 1

Related Questions