• 获取Assets目录下的图片并显示


     1 package com.jingle.getlocal;
     2 
     3 
     4 import java.io.InputStream;
     5 
     6 import android.app.Activity;
     7 import android.content.res.AssetManager;
     8 import android.graphics.Bitmap;
     9 import android.graphics.BitmapFactory;
    10 
    11 import android.os.Bundle;
    12 import android.widget.ImageView;
    13 
    14 public class MainActivity extends Activity {
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20 
    21         initImg();
    22 
    23     }
    24 
    25     private void initImg() {
    26         ImageView img = (ImageView) findViewById(R.id.img);
    27         img.setImageBitmap(getImg("orca.jpg"));//assets目录下文件名
    28     }
    29     //获取图片的Bitmap对象
    30     private Bitmap getImg(String file) {
    31         Bitmap bmp = null;
    32         //获取AssetsMng对象
    33         AssetManager am = getResources().getAssets();
    34         try {
    35             //打开文件,返回输入流
    36             InputStream is = am.open(file);
    37             //Bitmap工厂解码输入流,得到bmp对象
    38             bmp = BitmapFactory.decodeStream(is);
    39             is.close();
    40         } catch (Exception e) {           
    41             e.printStackTrace();
    42         }
    43         return bmp;
    44     }
    45 
    46 }
  • 相关阅读:
    SQliteDatabase详解
    Eclipse常用快捷键
    Android 省市区三级联动
    关于安卓9patch图片的探究
    9patch
    Day3_UI布局--FXQ
    day2-UI布局
    Day01_扩展_Genymotion模拟器的使用
    React Examples
    React项目结构
  • 原文地址:https://www.cnblogs.com/jinglecode/p/4374705.html
Copyright © 2020-2023  润新知