Android獲取圖片資源的4種方式

1. 圖片放在sdcard中,android

  Bitmap p_w_picpathBitmap = BitmapFactory.decodeFile(path) (path 是圖片的路徑,跟目錄是/sdcard)app


2. 圖片在項目的res文件夾下面ide


  //獲得application對象this

  ApplicationInfo appInfo = getApplicationInfo();code

  //獲得該圖片的id(name 是該圖片的名字,drawable 是該圖片存放的目錄,appInfo.packageName是應用程序的包)xml

  int resID = getResources().getIdentifier(name, "drawable", "appInfo.packageName");對象


  //代碼以下圖片

  public Bitmap getRes(String name)get

{it

   ApplicationInfo appInfo = getApplicationInfo();

   int resID = getResources().getIdentifier(name, "drawable", "appInfo.packageName");

   return BitmapFactory.decodeResource(getResources(), resID);

  }


3. 圖片放在src目錄下

  String path = com/xiangmu/test.png; //圖片存放的路徑

  InputStream is = getClassLoader().getResourceAsStream(path); //獲得圖片流


4.android中有個Assets目錄,這裏能夠存放只讀文件

方法1:

AssetManager assetManager = this.getResources().getAsset();

InputStream fis = assetManager.open("db.properties");

簡寫爲:

InputStream fis = getResources().getAssets().open("db.properties");

方法2:

InputStream abpath = getClass().getResourceAsStream("/assets/文件名");




5.訪問res/raw目錄中的文件(//res/raw目錄下存放,好比test.xml一個二進制文件,咱們能夠讀取能夠直接)

InputStream is=context.getResources().openRawResource(R.raw.test);

相關文章
相關標籤/搜索