『壹』 android如何從SD卡讀取圖片文件轉化為bitmap
SDK 中有專門取SD卡路徑的靜態方法
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState()
.equals(Android.os.Environment.MEDIA_MOUNTED); //判斷sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();//獲取跟目錄
}
return sdDir.toString();
}
不要寫死路徑
『貳』 android 已經知道路徑怎麼將路徑中的圖片變成Bitmap
/**
* 獲取本地圖片並指定高度和寬度
*/
public static Bitmap getNativeImage(String imagePath)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 獲取這個圖片的寬和高
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath, options); //此時返回myBitmap為空
//計算縮放比
int be = (int)(options.outHeight / (float)200);
int ys = options.outHeight % 200;//求余數
float fe = ys / (float)200;
if (fe >= 0.5)
be = be + 1;
if (be <= 0)
be = 1;
options.inSampleSize = be;
//重新讀入圖片,注意這次要把options.inJustDecodeBounds 設為 false
options.inJustDecodeBounds = false;
myBitmap = BitmapFactory.decodeFile(imagePath, options);
return myBitmap;
}
/**
* 以最省內存的方式讀取本地資源的圖片 或者SDCard中的圖片
* @param imagePath
* 圖片在SDCard中的路徑
* @return
*/
public static Bitmap getSDCardImg(String imagePath)
{
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//獲取資源圖片
return BitmapFactory.decodeFile(imagePath, opt);
}
『叄』 android中Bitmap存為一張圖片
可以用Bitmap.compress函數來把Bitmap對象保存成PNG或JPG文件,然後可以用BitmapFactory把文件中的數據讀進來再生成Bitmap對象。
保存的代碼大概類似於這樣:
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
具體的可以去查Bitmap和BitmapFactory的幫助文檔。
『肆』 Android ,base64轉bitmap
1 把圖像文件讀如byte數組中。
2 然後調用EncodeBase64函數,把Byte數組傳入,函數返回Base64的字元串。
以上即可完成Base64轉換。
反方向
1 然後調用DecodeBase64函數,把Byte64字元串傳入,函數返回Byte數組。
2 把Bye數組內容寫入文件,文件名為bitmap點陣圖的bpm文件即可。
『伍』 Android Bitmap 與 Drawable之間的區別和轉換
Android bitmap和drawable的區別和轉換如下:
1.bitmap 轉換 drawable
java">Bitmapbitmap=newBitmap(...);Drawabledrawable=newBitmapDrawable(bitmap);
//Drawabledrawable=newFastBitmapDrawable(bitmap);
2.Drawable to Bitmap
BitmapDrawable, FastBitmapDrawable直接用getBitmap
b. 其他類型的Drawable用Canvas畫到一個bitmap上
Canvascanvas=newCanvas(bitmap)
drawable.draw(canvas);
Drawabled=ImagesList.get(0);Bitmapbitmap=((BitmapDrawable)d).getBitmap();
區別如下:
1.Bitmap - 稱作點陣圖,一般點陣圖的文件格式後綴為bmp,當然編碼器也有很多如RGB565、RGB888。作為一種逐像素的顯示對象執行效率高,但是缺點也很明顯存儲效率低。
2.Drawable - 作為Android平下通用的圖形對象,它可以裝載常用格式的圖像,比如GIF、PNG、JPG,當然也支持BMP,當然還提供一些高級的可視化對象,比如漸變、圖形等。
另外還有如下相類似的格式:
Canvas - 名為畫布,可以看作是一種處理過程,使用各種方法來管理Bitmap、GL或者Path路徑,同時它可以配合Matrix矩陣類給圖像做旋轉、縮放等操作,同時Canvas類還提供了裁剪、選取等操作。
Paint - 可以把它看做一個畫圖工具,比如畫筆、畫刷。管理了每個畫圖工具的字體、顏色、樣式。
『陸』 如何把png文件轉為可用於android.graphics.BitmapFactory.decod
把包在linux下解包。改了在換進去
『柒』 求教,如何在Android中將圖片轉化為矩陣
Android本身的android.graphics.Bitmap實現
一般在Android使用圖片,都會用到這個類,這個類中有一個函數:
getPixels(int[] pixels, int offset, int stride, int x, int y, int width, int height)
Returns in pixels[] a of the data in the bitmap.
功能和你要求的類似,只不過返回的是一位數組,轉成二維數組應該不是問題吧。
使用是需注意,對於比較大的圖片,你的操作很可能會比較佔用內存,需要處理一下out of memory的exception。