我无法将我的JSON文件中的图片加载到Android中的列表视图
我知道我必须使用BitmapFactory.decodeStream,但除此之外我还不确定如何实现它
这是我的适配器和活动
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"/>
非常感谢任何帮助,谢谢
答案 0 :(得分:2)
愿此链接有助于使用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);
答案 1 :(得分:1)
您可以使用简单的库在Android ListView中显示图像。图像正在后台异步下载。图像正在缓存在SD卡和内存中。
从here
获取LazyListif (state.startWith(s)) {
stateArray.add(state.getName ()); // create getName method in State (like) getName in Cuty
}
不要忘记将以下权限添加到AndroidManifest.xml中:
ImageLoader imageLoader=new ImageLoader(context);
...
imageLoader.DisplayImage(url, imageView);