有時(shí)遇到了圖片保存到本地的操作,明明很久之前的時(shí)候搞過(guò),但是,每次還得重新打開瀏覽器搜索怎么操作,所以這次決定記錄下來(lái),爭(zhēng)取下次可以自己獨(dú)立寫出來(lái)
保存圖片
此時(shí)我們獲取到圖片的bitmap,然后調(diào)用方法將bitmap保存到本地
private void saveBitmap(Bitmap bitmap) {
String IMAGE_FILE_NAME = "screen_shot.jpg";
imgPath = Environment.getExternalStorageDirectory() + "/"
+ IMAGE_FILE_NAME; //此處是將圖片保存到根目錄
File f = new File(imgPath);
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
} catch (Exception e) {
e.printStackTrace();
}
try {
if (fOut != null)
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fOut != null)
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
保存gif圖
拿到gif圖的byte數(shù)組,然后將此數(shù)組寫到文件中
private void saveGif(byte[] bytes) {
String GIF_FILE_NAME = "screen_shot.gif";
imgPath = Environment.getExternalStorageDirectory() + "/"
+ GIF_FILE_NAME; //也是根目錄
File file = new File(imgPath);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes, 0, bytes.length);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}