Android 取得網路圖片並儲存到本地
1.註冊權限
<uses-permission android:name="android.permission.INTERNET"/>
2.設定儲存路徑
val netWorkFile = File(getExternalFilesDir("network"),
SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.TAIWAN).format(System.currentTimeMillis()) + ".jpg")
3.獲取圖片
val imgURLStr = "https://i-1-lanrentuku.qqxzb-img.com/2020/7/16/46fe692d-0438-4f08-bc56-03683fd2d65c.jpg?imageView2/2/w/1024/"
thread {
val imageBytes = URL(imgURLStr).readBytes()
val bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
val out = FileOutputStream(File(netWorkFile.absolutePath))
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)
}
![](https://badgameshow.com/fly/wp-content/uploads/2021/05/wp_editor_md_929e5cb65d3d9f3d1ce3260d67d7855c.jpg)