-1

By using getImage() I get the name of the file which is in assets/images folder, But I don't know how to create a bitmap. I cannot use getResorces(), because the class is not an Activity.

How can I create a bitmap having the name of the file stored in a string?

 public class ListAdapter extends BaseAdapter {
     ...
     ...
     ....

    private Bitmap getImage(Node node){
       String path = node.getFirstChild().getFirstChild().getNextSibling().getNextSibling().getNextSibling().getTextContent();
    }
}
Sanket Bhat
  • 340
  • 3
  • 16
AFS
  • 1,433
  • 6
  • 28
  • 52
  • 2
    Code how to load a file from assets has been posted a hundred times. Further you can supply your class with an activity context so that would be no problem then. – greenapps Nov 25 '17 at 12:22
  • https://stackoverflow.com/a/8501428/115145 – CommonsWare Nov 25 '17 at 12:24
  • `I cannot use getResorces(), because the class is not an Activity.` No, you can't retrieve the file through `getResources()` because it's in the `assets` folder. – Phantômaxx Nov 25 '17 at 13:02

1 Answers1

0

You can not call getResources() method without Context. So the best option to get a context in ListAdapter is creating a constructor that takes a context as an argument.

public class ListAdapter extends BaseAdapter {
    Context mContext;
    ListAdapter(Context context){
    mContext = context;
    }
    //Your code...
}

And if you have image in asset folder

Bitmap getImage(Node node){
    String fileName = node.getFirstChild().getFirstChild().getNextSibling().getNextSibling().getNextSibling().getTextContent();
    try {
        InputStream stream = mContext.getAssets().open(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(stream);
        return bitmap;
    }catch(IOException e) {
      //Handle this case
    }
return null;
}
Sanket Bhat
  • 340
  • 3
  • 16