-1

I am trying to make a simple app. I have a method for returning an InputStream and then I will bind this stream to an ImageView. But it doesn't work as always. There is no exception. What am I doing wrong. How can I make this code run?

String[] names;
Bitmap bmp;
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    AssetManager am=getAssets();
    img = (ImageView)findViewById(R.id.imageView1);
    try {
        names=am.list("myfiles");
        InputStream is=bitmapStream(names[1]);
        bmp =BitmapFactory.decodeStream(is);
        img.setImageBitmap(bmp);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public InputStream bitmapStream(String name) throws FileNotFoundException {
    InputStream is = null;
    is = openFileInput(name);   
    return is;
}
Boann
  • 48,794
  • 16
  • 117
  • 146
user3651582
  • 45
  • 1
  • 4

3 Answers3

0

Bitmaps can be null. I suggest using an asynchronous loading library like Picasso.

interlude
  • 843
  • 8
  • 29
0

Try loading a drawable from the inputStream. A similar question was answered here, it might help: How to load a image from assets?

Community
  • 1
  • 1
yunou
  • 1
0

The file that you're trying to load is not at the place were you try to load it from.

  try {
        String folder = "myfiles";
        names=am.list(folder);
        InputStream is= am.open(folder+"/"+names[0]);
        bmp =BitmapFactory.decodeStream(is);
        img.setImageBitmap(bmp);
    } 
Rob Anderson
  • 2,377
  • 3
  • 22
  • 31