gpt4 book ai didi

php - android从mysql数据库中获取图像列表

转载 作者:行者123 更新时间:2023-11-29 20:02:05 25 4
gpt4 key购买 nike

我可以使用异步任务从 mysql 数据库加载一个图像,但现在我想加载 5 个图像并在 ListView 中显示它们,所以我首先想知道如何使 doInbackground 方法 返回一个从 mysql 数据库中获取的图像列表:

 @Override
protected Bitmap doInBackground(String... params) {
String id = params[0];
String add = "http://192.168.1.11/save/load_image_from_db.php?id=" + id;
URL url;
Bitmap image = null;
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
//options.inJustDecodeBounds = true;
//options.inSampleSize = 4;
//options.inJustDecodeBounds = false;
url = new URL(add);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
image = BitmapFactory.decodeStream(connection.getInputStream(),null,options);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return image;
}

最佳答案

在这种情况下,你必须返回一个图像的 ArrayList,代码将是这样的,(注意:我没有测试过这段代码)

    @Override protected Bitmap doInBackground(String... params) {
String id = params[0];

String add1 = "http://192.168.1.11/save/load_image_from_db.php?id=" + id;
String add2="http://another photo";
String add3="http://another photo";
String[] adds={add1, add2, add3};
URL url;

ArrayList <Bitmap> imageList=new ArrayList();

Bitmap image = null;

for (int i=0;i<adds.length;i++){

try {
final BitmapFactory.Options options = new BitmapFactory.Options(); null //options.inJustDecodeBounds = true; //options.inSampleSize = 4; null//options.inJustDecodeBounds = false;

url = new URL(adds[i]); null HttpURLConnection connection =(HttpURLConnection) url.openConnection(); nullimage = BitmapFactory.decodeStream(connection.getInputStream(),null,options); null} catch (MalformedURLException e) { e.printStackTrace(); null} catch (IOException e) { e.printStackTrace(); null}

imageList.add( image);
image=null;
}
return imageList;
}

关于php - android从mysql数据库中获取图像列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36120798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com