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 }
时间: 2024-10-13 09:23:04