Leonardo Burroni
Leonardo Burroni

Reputation: 23

Android java.io.FileNotFoundException with Jpg

I have to draw an image located on asstes/images on a ImageView. First of all I save all the files in the assets/images folder on a String array.

try {
        String [] immages = assetManager.list("images");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

And with Log.d I can see that all the images are scanned correctly. Then I have this method that returns a Bitmap that will be set on a ImageView

private Bitmap readImg()
{
    InputStream inputStreamImages = null;
    Resources res = getResources();
    Bitmap bmp= null;
    try {

        inputStreamImages = assetManager.open(images[Integer.parseInt(number) - 1]);
        bmp = BitmapFactory.decodeStream(inputStreamImages);
        } catch(IOException e1) {e1.printStackTrace();}
    return bmp;
}

But I made another method because that one didn't worked.

private Drawable readImg2()
{
    Drawable d= null;
    try {
        InputStream inputStreamImages = getAssets().open(images[Integer.parseInt(number) - 1]);
        d = Drawable.createFromStream(inputStreamImages, null);
        }catch(IOException e1) {e1.printStackTrace();}
    return d;
}

The are very similar but both throw the same Error:

java.io.FileNotFoundException: v2.jpg

Can anyone help me? Thank you ;D

Upvotes: 2

Views: 591

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

images Array contains only image names with extension instead of image names with directory in which files are available so add images as prefix when passing to assetManager.open method as :

    InputStream inputStreamImages = 
                assetManager.open("images/"+images[Integer.parseInt(number) - 1]);

Upvotes: 1

Related Questions