Android 安卓将网络图片保存到相册
【摘要】 Android 安卓将网络图片保存到相册
封装了一个工具类!
请先加上读写权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_ST...
Android 安卓将网络图片保存到相册
封装了一个工具类!
请先加上读写权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- 1
- 2
如果是Android6.0之后,加上权限后再动态申请权限
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
- 1
- 2
- 3
- 4
- 5
使用方法:
//示例1:保存网络图片
SaveNetPhotoUtils.savePhoto(this, "https://profile.csdnimg.cn/5/F/3/3_qq_40881680");
//示例2:保存网络图片并且给图片命名
SaveNetPhotoUtils.savePhoto(this, "https://profile.csdnimg.cn/5/F/3/3_qq_40881680", "photoName.jpg");
- 1
- 2
- 3
- 4
- 5
- 6
该类所带工具:
SaveNetPhotoUtils.savePhoto(上下文, 网络图片url);
SaveNetPhotoUtils.savePhoto(上下文, 网络图片url, 图片名字 + 后缀);
- 1
- 2
工具类:
/**
* @author ThirdGoddess
* @email ofmyhub@gmail.com
* @Github https://github.com/ThirdGoddess
* @date :2020-03-14 10:11
*/
public class SaveNetPhotoUtils { private static Context contexts; private static String photoUrls; private static Bitmap bitmap; private static String mSaveMessage = "failed"; //自定义名字 private static String photoNames; /** * 保存图片,无须自定义名字 * * @param context * @param photoUrl */ public static void savePhoto(Context context, String photoUrl) { contexts = context; photoUrls = photoUrl; new Thread(saveFileRunnable).start(); } /** * 定义图片名字保存到相册 * * @param context * @param photoUrl * @param photoName 图片名字,定义格式 name.jpg/name.png/... */ public static void savePhoto(Context context, String photoUrl, String photoName) { contexts = context; photoUrls = photoUrl; photoNames = photoName; new Thread(saveFileRunnable2).start(); } private static Runnable saveFileRunnable = new Runnable() { @Override public void run() { try { if (!TextUtils.isEmpty(photoUrls)) { URL url = new URL(photoUrls); InputStream inputStream = url.openStream(); bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); } saveFile(bitmap); mSaveMessage = "success!"; } catch (IOException e) { mSaveMessage = "failed"; e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } messageHandler.sendMessage(messageHandler.obtainMessage()); } }; private static Runnable saveFileRunnable2 = new Runnable() { @Override public void run() { try { if (!TextUtils.isEmpty(photoUrls)) { URL url = new URL(photoUrls); InputStream inputStream = url.openStream(); bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); } saveFile(bitmap, photoNames); mSaveMessage = "success!"; } catch (IOException e) { mSaveMessage = "failed"; e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } messageHandler.sendMessage(messageHandler.obtainMessage()); } }; /** * 保存成功和失败通知 */ @SuppressLint("HandlerLeak") private static Handler messageHandler = new Handler() { @Override public void handleMessage(Message msg) { Toast.makeText(contexts, mSaveMessage, Toast.LENGTH_SHORT).show(); } }; /** * 保存图片 * * @param bm * @throws IOException */ public static void saveFile(Bitmap bm) throws IOException { File dirFile = new File(Environment.getExternalStorageDirectory().getPath()); if (!dirFile.exists()) { dirFile.mkdir(); } //图片命名 String fileName = UUID.randomUUID().toString() + ".jpg"; File myCaptureFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); bos.close(); //广播通知相册有图片更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(myCaptureFile); intent.setData(uri); contexts.sendBroadcast(intent); } /** * 保存图片 * * @param bm * @param photoName 图片命名 * @throws IOException */ public static void saveFile(Bitmap bm, String photoName) throws IOException { File dirFile = new File(Environment.getExternalStorageDirectory().getPath()); if (!dirFile.exists()) { dirFile.mkdir(); } //图片命名后保存到相册 File myCaptureFile = new File(Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/" + photoName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile)); bm.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); bos.close(); //广播通知相册有图片更新 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(myCaptureFile); intent.setData(uri); contexts.sendBroadcast(intent); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
文章来源: myhub.blog.csdn.net,作者:第三女神程忆难,版权归原作者所有,如需转载,请联系作者。
原文链接:myhub.blog.csdn.net/article/details/104855922
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)