注 :我是用Android Studio开发的,我是从网络获取图片数据的,在线等:
以下是代码:
package com.example.ai.helloworld;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.LinkedList;
import java.util.List;
import cn.trinea.android.common.service.impl.ImageCache;
import cz.msebera.android.httpclient.Header;
public class ListViewActivity extends BaseActivity {
//获取网络数据绑定
@Override
public String getTag() {
return "ListView";
}
//初始化数据
public void init(){
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.showfm.net/api/novel.asp",new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
//输出获取的数据
Log.d(ListViewActivity.this.getTag(),response.toString());
try {
JSONObject jsnobject = new JSONObject(String.valueOf(response));
JSONArray jsonarray = jsnobject.getJSONArray("novels");
for(int i = 0;i<jsonarray.length();++i){
JSONObject jnovel = jsonarray.getJSONObject(i);
ItemObj itemobj = new ItemObj(jnovel.getInt("id"),jnovel.getString("nj_name"),jnovel.getString("nj_avatar"));
datas.add(itemobj);
}
myAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listView = (ListView)findViewById(R.id.listview);
myAdapter = new MyAdapter();
listView.setAdapter(myAdapter);
imageCache = new ImageCache();
init();
myAdapter.notifyDataSetChanged();
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount(){
return datas.size();
}
@Override
public Object getItem(int position){
return datas.get(position);
}
@Override
public long getItemId(int position){
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
Holder holder;
if(convertView == null){
convertView = getLayoutInflater().inflate(R.layout.listview_item,null);
holder = new Holder();
holder.name = (TextView) convertView.findViewById(R.id.ls_tx);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
ItemObj item = (ItemObj)getItem(position);
imageCache.get(item.avatar,holder.img);
holder.name.setText(item.nj_name);
return convertView;
}
class Holder {
ImageView img;
TextView name;
}
}
class ItemObj{
public ItemObj(int novel_id,String name,String avatar) {
this.novel_id = novel_id;
this.nj_name = name;
this.avatar = avatar;
}
public int novel_id;
public String nj_name;
public String avatar;
}
ListView listView;
List<ItemObj> datas = new LinkedList<>();
MyAdapter myAdapter;
ImageCache imageCache;
}
提示的错误:
09-09 08:44:46.590 2622-3046/com.example.ai.helloworld E/ImageSDCardCache: get image exception while write to file, imageUrl is: http://www.showfm.net/api/avatar/Jummy.jpg, savePath is /storage/emulated/0/Trinea/AndroidCommon/ImageCache/http___www_showfm_net_api_avatar_Jummy_jpg
java.lang.RuntimeException: FileNotFoundException occurred.
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:247)
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:210)
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:197)
at cn.trinea.android.common.service.impl.ImageSDCardCache$3.onGetData(ImageSDCardCache.java:833)
at cn.trinea.android.common.service.impl.ImageSDCardCache$3.onGetData(ImageSDCardCache.java:809)
at cn.trinea.android.common.service.impl.PreloadDataCache$GetDataThread.run(PreloadDataCache.java:569)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.io.FileNotFoundException: /storage/emulated/0/Trinea/AndroidCommon/ImageCache/http___www_showfm_net_api_avatar_Jummy_jpg (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(FileOutputStream.java:221)
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:238)
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:210)
at cn.trinea.android.common.util.FileUtils.writeFile(FileUtils.java:197)
at cn.trinea.android.common.service.impl.ImageSDCardCache$3.onGetData(ImageSDCardCache.java:833)
at cn.trinea.android.common.service.impl.ImageSDCardCache$3.onGetData(ImageSDCardCache.java:809)
at cn.trinea.android.common.service.impl.PreloadDataCache$GetDataThread.run(PreloadDataCache.java:569)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
效果图片: