How to load a image from assets?

前端 未结 6 849
傲寒
傲寒 2020-11-29 06:28

i need to load a image from assets to avoid a froyo 2.2.2 bug resizing POT images in some particular cases. The way to avoid it is loading the image files from assets dir.

6条回答
  •  暖寄归人
    2020-11-29 06:47

    Another version, inside a fragment:

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    { root = inflater.inflate(R.layout.createrestaurant_layout, container, false);
      Resources res = getResources();
      img = (ImageView) root.findViewById(R.id.img);
      AssetManager amanager = res.getAssets();
      try {
          InputStream imageStream = amanager.open("restaurant.jpg");
          Drawable drawable = new BitmapDrawable(res, imageStream);
          img.setImageDrawable(drawable);
      }  catch (Exception e) { e.printStackTrace(); }
    } 
    

    This process seems much simpler in iOS/UIKit or iOS/SwiftUI.

提交回复
热议问题