0

I have a .png file in my assets folder and I'm trying to get that image to show on screen when I tap the screen.

InputStream open = null;
try{
    open = asset.open("ic_launcher.png");
    Bitmap bitmap = BitmapFactory.decodeStream(open);

    ImageView image = (ImageView)findViewById(R.id.imageView1);
    image.setImageBitmap(bitmap);
}catch(Exception e){
    e.printStackTrace();
}
finally{
    if(open != null){
        try{
            open.close();
        }catch(IOException e){
            e.printStackTrace();
        }       
    }
}

But I keep getting the FileNotFoundException and don't know why.

sakura
  • 2,249
  • 2
  • 26
  • 39
Space Ghost
  • 765
  • 2
  • 13
  • 26

4 Answers4

0

Check below line of code and replace it.

InputStream is = getAssets().open("ic_launcher.png");
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

use ,

 open =getAssets().open("icon.png");
0

Try this, it may work.

 open = asset.open
("ic_launcher");

don't mention extentions.

Emm Jay
  • 360
  • 2
  • 20
  • That didn't work .However, I cleaned the project and restarted it. Now I get a different error. I get an NPE error.. – Space Ghost Mar 31 '14 at 07:39
  • 2
    forgot about drawable and using assets? images you store in assets doesn't get compressed. Better to use drawable. – Emm Jay Mar 31 '14 at 07:42
0

Try this-

try {
InputStream ims = getAssets().open("ic_launcher.png");
Drawable d = Drawable.createFromStream(ims, null);
imgView.setImageDrawable(d);
}
catch(IOException ex) {
return;
}

Also check this.

Community
  • 1
  • 1
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21