android网络图片保存本地工具,Android 保存图片到本地(亲测有效)

/**

* 将Bitmap图片保存到本地相册

*/

public static void savePhotoToGallery(final Context context, final Bitmap bitmap) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

AndPermission.with((Activity) context)

.requestCode(200)

.permission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)

.start();

}

if (bitmap == null) {

ToastUtil.showCenterToast(context, "未获取到图片");

return;

}

new Thread(new Runnable() {

@Override

public void run() {

// 其次把文件插入到系统图库

try {

MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap,

fileName, "测试 图集"); // 名字和描述没用,系统会自动更改

((Activity) context).runOnUiThread(new Runnable() {

@Override

public void run() {

ToastUtil.showCenterToast(context, "图片保存至相册");

}

});

} catch (Exception e) {

((Activity) context).runOnUiThread(new Runnable() {

@Override

public void run() {

ToastUtil.showCenterToast(context, "图片保存失败");

}

});

LogUtils.e("图片保存异常:", e);

}

}

}).start();

}

/**

* 将图片保存到本地相册

*

*/

public static void savePhotoToGallery(final Context context, final String imgUrl) {

if (TextUtils.isEmpty(imgUrl)) {

ToastUtil.showCenterToast(context,"未获取到图片");

return;

}

new Thread(new Runnable() {

@Override

public void run() {

String fileName = "test_" + System.currentTimeMillis() + ".jpg";

String sdCardDir = SDCardUtils.getDiskDir() + "DCIM/";

File appDir = new File(sdCardDir, "text");

if (!appDir.exists()) {

appDir.mkdir();

}

File f = new File(appDir, fileName);

try {

// 保存图片

URL url = new URL(imgUrl);

HttpURLConnection con = (HttpURLConnection) url.openConnection();

con.setRequestMethod("GET");

con.setConnectTimeout(1000 * 6);

if (con.getResponseCode() == 200) {

InputStream inputStream = con.getInputStream();

byte[] b = FileUtils.getBytes(inputStream);

FileOutputStream fileOutputStream = new FileOutputStream(f);

fileOutputStream.write(b);

fileOutputStream.close();

} else {

ToastUtil.showCenterToast(context,"图片保存失败");

return;

}

//把文件插入到系统图库

MediaStore.Images.Media.insertImage(context.getContentResolver(),

f.getAbsolutePath(), fileName, null);

// 通知图库更新

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(f.getPath()))));

((Activity) context).runOnUiThread(new Runnable() {

@Override

public void run() {

ToastUtil.showCenterToast(context,"图片保存至相册");

}

});

} catch (Exception e) {

((Activity) context).runOnUiThread(new Runnable() {

@Override

public void run() {

ToastUtil.showCenterToast(context,"图片保存失败");

}

});

LogUtils.e("图片保存异常:", e);

}

}

}).start();

}

其中的getBytes方法如下:

/**

* 将InputStream,转换为字节

*/

public static byte[] getBytes(InputStream inputStream) throws Exception {

byte[] b = new byte[1024];

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

int len = -1;

while ((len = inputStream.read(b)) != -1) {

byteArrayOutputStream.write(b, 0, len);

}

byteArrayOutputStream.close();

inputStream.close();

return byteArrayOutputStream.toByteArray();

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值