Home  >  Article  >  Java  >  Android loads the image under the asset folder

Android loads the image under the asset folder

高洛峰
高洛峰Original
2017-02-11 16:34:011363browse

Load the image files in the asset into the ImageView

// load image 
    try { 
      // get input stream 
      InputStream ims = getAssets().open("avatar.jpg"); 
      // load image as Drawable 
      Drawable d = Drawable.createFromStream(ims, null); 
      // set image to ImageView 
      mImage.setImageDrawable(d); 
    } 
    catch(IOException ex) { 
      return; 
    }

Draw the image files in the asset into the custom View.

Bitmap bitmap;
    try { 
      InputStream ims = this.getContext().getAssets().open("fl.jpg"); 
      // 读入图片并将其强转为 BitmapDrawable类型
      BitmapDrawable bd = (BitmapDrawable) Drawable.createFromStream(ims, null);
      bitmap = bd.getBitmap();
      ims.close();
    } 
    catch(IOException ex) { 
      return; 
    } 
    //canvas.drawBitmap(bitmap, -200, -200, new Paint());
    canvas.drawBitmap(bitmap, null, new Rect(-30,-40,30,40), new Paint());//null表示原图尺寸,第二个rect表示显示区域(位图会拉伸填充该区域)

The above is the picture below the Android loading asset folder introduced by the editor. I hope it will be helpful to you. If you have any questions Please leave me a message and I will reply to you in time. I would also like to thank everyone for your support of the Script House website!

For more articles related to Android loading the pictures under the asset folder, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn