1:根據圖片在本地上的絕對路徑獲取圖片並轉換成Bitmap:
private Bitmap getDiskBitmap(String pathString)
{
Bitmap bitmap = null;
try
{
File file = new File(pathString);
if(file.exists())
{
bitmap = BitmapFactory.decodeFile(pathString);
}
} catch (Exception e)
{
// TODO: handle exception
}
return bitmap;
}
2:通過Intent在Activity之間傳遞Bitmap:
之前想通過一個自定義類實現Serializable接口來直接實現intent傳遞自定義對象,結果項目報錯,查看了原因才知道我的自定義類中含有bitmap變量,而Bitmap變量是不能被序列化的,所以無法通過intent來直接傳遞整個對象,所以我採取了分開傳值:
傳送方:
intent.putExtra("image", bmp);
接受方:
Bitmap bmp = getIntent().getParcelableExtra("image")
3:將bitmap以數種格式保存在本地:
private void savePreviewBitmap(File file,Bitmap photoBm,String fileName){
createFile();
file = new File(Environment.getExternalStorageDirectory().getPath() + "/cardBook/" + fileName + ".jpg");
if(file.exists()){
file.delete();
}
try{
file.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
FileOutputStream fOut = null;
try{
fOut = new FileOutputStream(file);
}catch(FileNotFoundException e){
e.printStackTrace();
}
photoBm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try{
fOut.flush();
fOut.close();
}catch(IOException e){
e.printStackTrace();
}
}
在CompressFormat.XXX中自己選擇格式