android map url,Android url转为bitmap 并保存到相册

public void url2bitmap(String url) {

Bitmap bm = null;

try {

URL iconUrl = new URL(url);

URLConnection conn = iconUrl.openConnection();

HttpURLConnection http = (HttpURLConnection) conn;

int length = http.getContentLength();

conn.connect();

// 获得图像的字符流

InputStream is = conn.getInputStream();

BufferedInputStream bis = new BufferedInputStream(is, length);

bm = BitmapFactory.decodeStream(bis);

bis.close();

is.close();

if (bm != null) {

save2Album(bm,url);

}

} catch (Exception e) {

runOnUiThread(new Runnable() {

@Override

public void run() {

showToastMessage("保存失败");

}

});

e.printStackTrace();

}

}

private void save2Album(Bitmap bitmap, String picUrl) {

File appDir = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".png");

if (!appDir.exists()) appDir.mkdir();

String[] str = picUrl.split("/");

String fileName = str[str.length - 1];

File file = new File(appDir, fileName);

try {

FileOutputStream fos = new FileOutputStream(file);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);

fos.flush();

fos.close();

onSaveSuccess(file);

} catch (IOException e) {

runOnUiThread(new Runnable() {

@Override

public void run() {

showToastMessage("保存失败");

}

});

e.printStackTrace();

}

}

private void onSaveSuccess(final File file) {

runOnUiThread(new Runnable() {

@Override

public void run() {

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

showToastMessage("保存成功");

}

});

}

Url 转化为Bitmap

private static Bitmap bitmap;

public static Bitmap getBitmap(final String url) {

new Thread(new Runnable() {

@Override

public void run() {

URL imageurl = null;

try {

imageurl = new URL(url);

} catch (MalformedURLException e) {

e.printStackTrace();

}

try {

HttpURLConnection conn = (HttpURLConnection)imageurl.openConnection();

conn.setDoInput(true);

conn.connect();

InputStream is = conn.getInputStream();

bitmap = BitmapFactory.decodeStream(is);

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}).start();

return bitmap;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值