简体   繁体   中英

How to open an image that is stored in a folder under Assets in android

I have created an assets folder directory under App with Build, Libs, and Src in android studio, I have placed folders in it with images in each respectively. I have an issue that I cannot find the file in with that filename or path but I know its correct, please tell me what to do here as I am stumped. The file is in this directory assets/profileicon/26.png and the number is determined by the profileIconTag (in this case its 26) now am I doing the right path name for the .open()?

AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open("/profileicon/" + profileIconTag + ".png");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);

    profileIcon.setImageBitmap(bitmap);

Check if the assets folder is in the correct path.

InputStream bitmap=null;
try {
    bitmap=getAssets().open(profileIconTag +".png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);
    profileIcon.setImageBitmap(bit);
} catch (IOException e) {
    e.printStackTrace();
} finally {
   try{
    if(bitmap!=null)
    bitmap.close();
   }catch(Exception ex) {}
}

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap. Check this link .

private Bitmap getBitmapFromAsset(String strName)
{
    AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open(strName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM