private void loadImageFromUrl(String imageUrl, ImageView imageView) {
new Thread(() -> {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
final Bitmap bitmap = BitmapFactory.decodeStream(input);
// 在 UI 线程更新 ImageView
imageView.post(() -> imageView.setImageBitmap(bitmap));
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
// AsyncTask 用于加载网络图片
private static class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
LoadImageTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(String... strings) {
String imageUrl = strings[0];
Bitmap bitmap = null;
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && imageViewReference.get() != null) {
ImageView imageView = imageViewReference.get();
imageView.setImageBitmap(bitmap);
}
}
}
06-05
1145

06-05
1694


c#打开外部文件
04-19
252


C#修改文件扩展名代码
04-19
375

04-19
221


jsoup获取图片教程
04-19
435

04-19
544

04-11
131


2024年安卓图片上传
04-11
185


安卓获取手机所有图片代码
04-11
232
