gpt4 book ai didi

java - 如何在android中转换图像url字符串

转载 作者:行者123 更新时间:2023-12-01 18:25:06 28 4
gpt4 key购买 nike

我有一个图像网址列表(100 个),我想将其与文本一起显示在 ListView 中。我遇到的问题是我的 ListView 将所有 URL 显示为字符串而不是图像。我已经实现了一个 BITMAP 类,并使用单个图像 url 对其进行了测试,并且在 IMAGEVIEW 上,它有效。我读过一些文章,但无法真正弄清楚如何使用 ListView。 How to load an ImageView by URL in Android?

在下面的代码中,imageUrls 将所有图像 URL 保存为字符串,concatString 保存 img +适配器中使用的文本。我的问题是如何在将 BITMAP 类放入适配器之前将其应用到包含所有 url 的 STRING 上,即 imageUrls?或者有更好的方法吗?

Bitmap bitmap;
String temp, json, namesImg, imageUrls, allImageANDnames;
StringBuffer sb = new StringBuffer();
ArrayList<String> concatString = new ArrayList<String>(); // ARRAYLIST THAT HOLDS IMG & TXT
JSONObject object;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);

// Reading json file from assets folder
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"myjson.json")));
while ((temp = br.readLine()) != null)
sb.append(temp);

json = new String(sb);
JSONArray jsonArray = new JSONArray(json);

for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
object = jsonArray.getJSONObject(i);
namesImg = object.getString("name"); // holds all text
imageUrls = object.getString("url"); // holds all image urls (HERE IS WHERE I NEED HELP)
allImageANDnames = imageUrls + "\t\t" + namesImg; // holds text and img urls
concatString.add(allImageANDnames); //(I WANT THIS TO HOLD IMAGES & TEXT) not urls & text
}

} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}finally {
if ((br != null)) {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
}

// adapter to show list in activity
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, concatString); // I USED THE HOLDER HERE

ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter); // LIST THAT POPULATE THE IMG & TXT
}

// HOW CAN I APPLY THIS CLASS ON ***imageUrls***
public class ImageFromUrl extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public GetImageFromUrl(ImageView img){
this.imageView = img;
}
@Override
protected Bitmap doInBackground(String... url) {
String stringUrl = url[0];
bitmap = null;
InputStream inputStream;
try {
inputStream = new java.net.URL(stringUrl).openStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
}
}

.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>

抱歉,我对此不太了解。

最佳答案

使用 ImageView 通过 recyclerview 而不是 listview 显示图像。

解决办法很简单。使用库(如 glide、Picasso 等)将 URL 转换为图像并设置到该位置。Picasso.get()
.load(网址)
.调整大小(50, 50)
.centerCrop()
.into(imageView)

Use this Library Picasso

关于java - 如何在android中转换图像url字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60241280/

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