android 保存网络图片到本地

╰+攻爆jí腚メ 2022-08-21 12:47 432阅读 0赞
  1. //是否存在sdcard.
  2. public boolean isSdcard() {
  3. try {
  4. return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
  5. } catch (Exception e) {
  6. e.printStackTrace();
  7. }
  8. return false;
  9. }
  10. public String saveToSdCard(Bitmap bitmap, String filename) {
  11. String path = null;
  12. File file;
  13. if (isSdcard()) {
  14. file = new File(Environment.getExternalStorageDirectory() + "noClear/capture/photos/", filename + ".jpg");
  15. //如果文件夹不存在则创建
  16. if (!file.exists() && !file.isDirectory()) {
  17. file.mkdirs();
  18. }
  19. } else {
  20. file = new File(Environment.getRootDirectory() + "noClear/capture/photos/", filename + ".jpg");
  21. //如果文件夹不存在则创建
  22. if (!file.exists() && !file.isDirectory()) {
  23. file.mkdirs();
  24. }
  25. }
  26. try {
  27. FileOutputStream out = new FileOutputStream(file);
  28. bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
  29. out.flush();
  30. out.close();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return file.getPath();
  35. }

发表评论

表情:
评论列表 (有 0 条评论,432人围观)

还没有评论,来说两句吧...

相关阅读

    相关 js保存图片本地

    说明:可以实现下载图片,但是移动端浏览器不行,暂时没有实现,如果有知道的大哥留个言,让我改进一下,感谢。 操作步骤: 新建一个文件夹,将代码放入index.html,然后在文

    相关 PHP抓取远程图片本地保存

    最近在工作中需要开发了一个用户素材功能,里面需要将网上的各种图片素材进行本地化存储。于是在网上找了一些相关资料,并根据自身开发需要,整理了一下主要的逻辑代码。 /