//壓縮圖片
public Bitmap compressBitmap(int i) throws IOException {
// 尺寸壓縮倍數,值越大,圖片尺寸越小
int ratio = 2;
// 壓縮Bitmap到對應尺寸
Bitmap bmp =BitmapFactory.decodeByteArray(items.get(i).getImageAsBytes(), 0, items.get(i).getImageAsBytes().length);
Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
canvas.drawBitmap(bmp, null, rect, null);
return result;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String path = Environment.getExternalStorageDirectory()+ "/Image/";
File dirFile = new File(path); //目錄轉化成文件夾
if (!dirFile .exists()) { //如果不存在,建立這個文件夾
dirFile .mkdirs();
}
//保存圖片到文件夾
File file = new File(path, items.get(i).getMessage()+ ".jpg");
try {
FileOutputStream out = new FileOutputStream(file);
// 把壓縮后的數據存放到out中
result.compress(Bitmap.CompressFormat.JPEG, 100 ,out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
照片壓縮
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- iOS 選擇照片、視頻、拍照并壓縮視頻獲取壓縮保存后路徑的方法 #import"ViewController.h"...
- 一款用于在Android設備上獲取照片(拍照或從相冊、文件中選擇)、裁剪圖片、壓縮圖片的開源工具庫https://...
- TakePhoto 簡介 TakePhoto是一款用于在Android設備上獲取照片(拍照或從相冊、文件中選擇)、...
- 網上找了很多的資料貌似都沒有很好的解決這個問題。先簡單的描述下,照片是我從服務器獲取到的,一個大小為8M的jpg圖...