gpt4 book ai didi

android - 将图像从 JSON 解析为 Android 中的 ListView

转载 作者:行者123 更新时间:2023-11-29 17:27:22 25 4
gpt4 key购买 nike

我无法将图像从我的 JSON 文件加载到 Android 中的 ListView

我知道我必须使用 BitmapFactory.decodeStream 但除此之外我不确定如何实现它

这是我的适配器和 Activity

public class JSON {

static InputStream is = null;

static String jsonString = "";

static JSONObject jObj = null;

public JSON() {

}

public JSONObject getJSONFromUrl(String urlString) {

try {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
is = con.getInputStream();
} catch (Exception e) {
Log.v("NETWORK ERROR #1", e.getMessage());
}

StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is));

String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.v("NETWORK ERROR #2 ", e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.v("NETWORK ERROR #3", e.getMessage());
}

jsonString = sb.toString();
}
}

try {
jObj = new JSONObject(jsonString);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

return jObj;
}
}

public class Main2Activity extends ListActivity {

//JSON URL
private static String url = "http://pastebin.com/raw.php?i=8eTR5TXv";

private static final String ALBUMS = "album";

private static final String C_ALBUMS = "c_album";
private static final String C_ARTISTS = "c_artist";
private static final String C_DATES = "c_date";
private static final String C_RATINGS = "c_rating";
private static final String C_PICS = "c_pic";

JSONArray albumType = null;

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

//Turn off StrictMode
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

JSON jParser = new JSON();

JSONObject json = jParser.getJSONFromUrl(url);

try {
albumType = json.getJSONArray(ALBUMS);

for(int i = 0; i < albumType.length(); i++){
JSONObject a = albumType.getJSONObject(i);

String cAlbum = a.getString(C_ALBUMS);
String cArtist = a.getString(C_ARTISTS);
String cDate = a.getString(C_DATES);
String cRating = a.getString(C_RATINGS);
String cPic = a.getString(C_PICS);

HashMap<String, String> map = new HashMap<String, String>();

map.put(C_ALBUMS, cAlbum);
map.put(C_ARTISTS, cArtist);
map.put(C_DATES, cDate);
map.put(C_RATINGS, cRating);
map.put(C_PICS, cPic);

contactList.add(map);
}

} catch (JSONException e) {
e.printStackTrace();
}

ListAdapter adapter = new SimpleAdapter(
this,
contactList,
R.layout.row2,
new String[] { C_ALBUMS, C_ARTISTS, C_DATES, C_RATINGS, C_PICS },
new int[] {R.id.tv_albumName, R.id.tv_Artist, R.id.tv_Date, R.id.tv_Rating, R.id.tv_pic}
);

setListAdapter(adapter);
}

和 XML

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ALBUM_NAME"
android:id="@+id/tv_albumName"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ARTIST"
android:id="@+id/tv_Artist"
android:textSize="12sp"
android:layout_below="@+id/tv_albumName"
android:layout_alignLeft="@+id/tv_albumName"
android:layout_alignStart="@+id/tv_albumName" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/DATE"
android:id="@+id/tv_Date"
android:textSize="12sp"
android:layout_below="@+id/tv_Artist"
android:layout_alignLeft="@+id/tv_Artist"
android:layout_alignStart="@+id/tv_Artist" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/RATING"
android:id="@+id/tv_Rating"
android:textSize="12sp"
android:layout_below="@+id/tv_Date"
android:layout_alignLeft="@+id/tv_Date"
android:layout_alignStart="@+id/tv_Date" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_pic"
android:layout_alignBottom="@+id/tv_Date"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:padding="1sp"/>

非常感谢任何帮助,谢谢

最佳答案

你只需要使用像Picasso 这样的Image Loader 库, Glide .

希望此链接有助于使用 Introduction to Glide, Image Loader Library for Android, recommended by Google

毕加索:

Picasso.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);

滑行:

Glide.with(context)
.load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
.into(ivImg);

或者你可以试试Universal Image Loader Library in Android

//your image url
String url = "Your Image URL";

ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisc(true).resetViewBeforeLoading(true)
.showImageForEmptyUri(fallback)
.showImageOnFail(fallback)
.showImageOnLoading(fallback).build();

//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView1)

//download and display image from url
imageLoader.displayImage(url, imageView, options);

关于android - 将图像从 JSON 解析为 Android 中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170819/

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