您的位置:首页 > 移动开发 > Android开发

android保存图片到本地

2015-12-22 11:18 375 查看
我将其写到了一个异步线程中:

class SaveImage extends AsyncTask<Void,Void,Void>{

@Override

protected Void doInBackground(Void... params) {

//保存到本地

FileOutputStream fos = null;

BufferedOutputStream bos = null;

ByteArrayOutputStream baos = null;

isUploading = true;

try{

baos = new ByteArrayOutputStream();

cutBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);

byte[] byteArray = baos.toByteArray();

// 将字节数组写入到刚创建的图片文件中

imgFile = new File(FileManager.getSaveFilesPath(),UUID.randomUUID().toString()+".png");

fos = new FileOutputStream(imgFile);

bos = new BufferedOutputStream(fos);

bos.write(byteArray);

}catch(Exception e){

showShortToast("图片上传失败");

}finally{

if(ValidateUtils.isNotNullObj(baos)) {

try {

baos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(ValidateUtils.isNotNullObj(bos)) {

try {

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if(ValidateUtils.isNotNullObj(fos)) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return null;

}

@Override

protected void onPostExecute(Void result) {

tvProgress.setVisibility(View.VISIBLE);

uploadingProgressView.setVisibility(View.VISIBLE);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: