AsynchttpClient下载文件,以文件流的形式

 /**
* 保存文件 以文件流的形式
*/

Bitmap bitmap = BitmapFactory.decodeByteArray(binaryData, 0, binaryData.length);
File directory = new File(Environment.getExternalStorageDirectory(), "asynchttp");
if (!directory.exists()) {
directory.mkdirs();//创建文件夹 MKDIRS S
}

File file = new File(directory, "picture" + System.currentTimeMillis() + ".jpg");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 压缩格式
Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
// 压缩比例
int quality = 100;

if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
OutputStream stream = new FileOutputStream(file);
bitmap.compress(format, quality, stream);//压缩格式 压缩比例 输出流
stream.close();
Log.e(TAG, "onSuccess: "+"保存成功" );
} catch (IOException e) {
e.printStackTrace();
}
posted @ 2017-10-28 11:03  阿金好掂  阅读(1548)  评论(0编辑  收藏  举报