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

孤街浪徒 提交于 2019-12-24 10:58:09

问题


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:

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


回答1:


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();
}



回答2:


Where would you like to put your "logo.jpg" file?

  1. If you would like to put it in the drawable folder, then use:

    BitmapFactory.decodeResource(getResources(), R.drawable.logo);
    
  2. If you would like to put it somewhere on your device's memory, then use:

    BitmapFactory.decodeResource("/sdcard/logo.jpg");
    

And here's some guide to help you start: http://developer.android.com/reference/android/graphics/BitmapFactory.html



来源:https://stackoverflow.com/questions/15939733/where-do-i-put-the-image-resource-file-bitmapfactory-decodefile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!