獲取bitmap的幾種方式總結
從獲取方式分:
(1)以檔案流的方式
假設在sdcard下有 test.png
fileinputstream fis = new fileinputstream("/sdcard/test.png");
bitmap bitmap=bitmapfactory.decodestream(fis);
(2)以r檔案的方式
假設 res/drawable下有 test.jpg檔案
bitmap bitmap =bitmapfactory.decoderesource(getresources(), r.drawable.test);
或bitmapdrawable bitmapdrawable = (bitmapdrawable) getresources().getdrawable(r.drawable.test);
bitmap bitmap = bitmapdrawable.getbitmap();
(3)以resourcestream的方式,不用r檔案
bitmap bitmap=bitmapfactory.decodestream(getclass().getresourceasstream(「/res/drawable/test.png」));
(4)以檔案流+r檔案的方式
inputstream in = getresources().openrawresource(r.drawable.test);
bitmap bitmap = bitmapfactory.decodestream(in);
或inputstream in = getresources().openrawresource(r.drawable.test);
bitmapdrawable bitmapdrawable = new bitmapdrawable(in);
bitmap bitmap = bitmapdrawable.getbitmap();
注意:openrawresource可以開啟drawable, sound, 和raw資源,但不能是string和color。
從資源存放路徑分:
(1)放在sdcard中
bitmap imagebitmap = bitmapfactory.decodefile(path);// (path 是的路徑,跟目錄是/sdcard)
(2)在專案的res資料夾下面
bitmap imagebitmap2 = bitmapfactory.decoderesource(getresources(), resid);
(3)放在src目錄下
string path = "com/xiangmu/test.png"; //存放的路徑
inputstream in = getclassloader().getresourceasstream(path); //得到流
bitmap imagebitmap3 = bitmapfactory.decodestream(in);
(4)放在assets目錄
inputstream in = getresources().getassets().open(filename);
bitmap imagebitmap4 = bitmapfactory.decodestream(in);
android中bitmap用法補充
android學習之位圖bitmap bitmap代表一張位圖,副檔名可以是.bmp或者.dib。點陣圖是windows標準格式圖形檔案,它將影象定義為由點 畫素 組成,每個點可以由多種色彩表示,包括2 4 8 16 24和32位色彩。例如,一幅1024 768解析度的32位真彩,其所佔儲存位元組數...
Android中Bitmap大小計算
bitmap.config是bitmap中的乙個內部類,在bitmap類裡createbitmap intwidth,int height,bitmap.config config 方法裡會用到,開啟個這個類一看 public static final bitmap.config alpha 8 p...
android獲取bitmap的方法
第一種方法 通過bitmapdrawable物件獲得bitmap 得到resources物件 resources r this.getcontext getresources 以資料流的方式讀取資源 inputstream is r.openrawresource r.drawable.my bac...