Android中圖片轉化為bitmap
阿新 • • 發佈:2019-01-24
圖片檔案轉為Bitmap物件
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath);
如果圖片過大,可能導致Bitmap物件裝不下圖片
解決辦法:
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //將圖片的長和寬縮小味原來的1/2
private BitmapFactory. Options getBitmapOption(int inSampleSize){
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = inSampleSize;
return options;
}
Bitmap物件儲存味圖片檔案
public void saveBitmapFile(Bitmap bitmap){
File file=new File("/mnt/sdcard/pic/01.jpg");//將要儲存圖片的路徑
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath);
如果圖片過大,可能導致Bitmap物件裝不下圖片
解決辦法:
String filePath="c:/01.jpg";
Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //將圖片的長和寬縮小味原來的1/2
private BitmapFactory. Options getBitmapOption(int inSampleSize){
System.gc();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inSampleSize = inSampleSize;
return options;
}
Bitmap物件儲存味圖片檔案
public void saveBitmapFile(Bitmap bitmap){
File file=new File("/mnt/sdcard/pic/01.jpg");//將要儲存圖片的路徑
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}