讀取assets文件夾中的圖片代碼

jopen 10年前發布 | 23K 次閱讀 assets Android開發 移動開發

先將圖片放到assets文件夾下

private Bitmap[] mBitmap = null;

// //根據文件名讀取assets文件夾的圖片

private Bitmap getBit(String filename){

//以最省內存的方式讀取本地資源的圖片 BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 2;//圖片寬高都為原來的二分之一,即圖片為原來的四分之一 options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; AssetManager asm = getAssets(); InputStream is; Bitmap bitmap = null; try { is = asm.open(filename); bitmap = BitmapFactory.decodeStream(is , null, options); // mImageView.setImageBitmap(bitmap); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap;

}

//初始化圖片容器

private void getbitmap(){ mBitmap = new Bitmap[14]; // mBitmap[0] = getBit("img1.jpg");

    for(int i=0; i<14; i++){
        int b = i+1;
        String s = "img"+b+".jpg";
        mBitmap[i] = getBit(s);
    }
}


// 獲取string中的string-array Resources res = getResources(); String [] arr = res.getStringArray(R.array.arr_name);</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!