Search code examples
androidbitmapresourcesdirectorybitmapfactory

Where do I put the image resource file? BitmapFactory.decodeFile()


I am trying to use this:

BitmapFactory.decodeFile("logo.jpg")

and I don't seem to have the file: logo.jpg in the right place in comparison to my apk. where should it go?

P.S. the error I get is:enter image description here

Edit

I now am using this: >BitmapFactory.decodeResource(getActivity().getResources(),"logo.jpg")

and am now getting a compiler error that says:

The method getActivity() is undefined for the type Brick (Brick is the name of the class)

I don't care which solution works as long as one of them does


Solution

  • I think what you are trying to do is to put an JPG file into your project and load via Java code, is that correct.

    If so, you'll need to put your logo.jpg into your assets folder,

    and load it using the similar method as this SO answer stated:

    https://stackoverflow.com/a/8502231/763459


    For your convenience I pasted the code as below:

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