//保存gif图片
new Thread(new Runnable() {
@Override
public void run() {
try {
Utils.saveGifFile(origUrl);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
//保存普通图片
new Thread(new Runnable() {
@Override
public void run() {
try {
Utils.saveFile(bitmap,String.valueOf(System.currentTimeMillis()));
Message msg = handler.obtainMessage(PICTURE_SAVE_SUCCESS);
handler.sendMessage(msg);
} catch (IOException e) {
Message msg = handler.obtainMessage(PICTURE_SAVE_FALSE);
handler.sendMessage(msg);
e.printStackTrace();
}
}
}).start();
public final static String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";//保存到SD卡
public final static String SAVE_REAL_PATH = SAVE_PIC_PATH + "/mker_picture";//保存图片的确切位置
/**
* 保存普通图片文件
*
* @param bm
* @param fileName
* @throws IOException
*/
public static void saveFile(Bitmap bm, String fileName) throws IOException {
File directoryFile = new File(Constant.SAVE_REAL_PATH);
if (!directoryFile.exists()) {
directoryFile.mkdirs();
}
File myCaptureFile = new File(directoryFile, fileName);
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}
//new FileOutputStream(myCaptureFile) 获取要保存到的文件的文件流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);//把图片压缩成jpeg格式的
bos.flush();//刷新流
bos.close();
}
/**
* 保存gif图片文件
*
* @param oldPath
* @throws IOException
*/
public static void saveGifFile(String oldPath) throws IOException {
File directoryFile = new File(Constant.SAVE_REAL_PATH);
if (!directoryFile.exists()) {
directoryFile.mkdirs();
}
File newFile = new File(directoryFile, String.valueOf(System.currentTimeMillis()) + ".gif");
if (!newFile.exists()) {
newFile.createNewFile();
}
String newPath = newFile.getPath();
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}