2 baidu 35975930 baidu_35975930 于 2017.09.09 16:55 提问

Android的ListView读取不了图片

注 :我是用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) 

效果图片:

图片说明

7个回答

ziyejinwei1994
ziyejinwei1994   2017.09.09 17:17
已采纳

getview里面初始化图片控件呢?都没有

ziyejinwei1994
ziyejinwei1994 回复阴鸦: 是在代码里面的findViewById 你的name都写了 怎么不写imageView
2 个月之前 回复
baidu_35975930
baidu_35975930 <ImageView android:id="@+id/pg_img" android:layout_width="match_parent" android:layout_height="match_parent"/>
2 个月之前 回复
baidu_35975930
baidu_35975930 有的!我发给你看看
2 个月之前 回复
baidu_35975930
baidu_35975930   2017.09.09 16:58

怎么没有大神出来帮我解决问题,自己顶一个

ziyejinwei1994
ziyejinwei1994   2017.09.09 17:24

还有ImageCache 这个你写入图片出错 了吧 怎么不用第三方来展示网络图片Glide很好用

ziyejinwei1994
ziyejinwei1994 回复阴鸦: http://blog.csdn.net/shangmingchao/article/details/51125554/
2 个月之前 回复
baidu_35975930
baidu_35975930 大神,我不会用第三方,我刚刚学习Android的,你发个第三方给我看看行不行
2 个月之前 回复
qq_33495943
qq_33495943   2017.09.09 17:53

主要错误在,你读取 图片路径时候,报错了,sd卡路径找不到,你获取的图片,

解决方案:
直接用Picasso 加载你获取到的图片路径就可以显示了。
Picasso.with(context).load(url).placeholder(R.drawable.tab_item_bg).into(imageView);

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

Notzuonotdied
Notzuonotdied   2017.09.09 21:36

给权限了没有?“No such file or directory”错误是这个。有可能是你没有给权限,也有可能使你的路径写错了

baidu_35975930
baidu_35975930 路径应该不会错的
2 个月之前 回复
baidu_35975930
baidu_35975930 <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2 个月之前 回复
baidu_35975930
baidu_35975930 给了的,
2 个月之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2017.09.10 19:22

你试试Picasso或者Glide

dongfeng9ge
dongfeng9ge 回复阴鸦: 百度会不会?
2 个月之前 回复
baidu_35975930
baidu_35975930 大神,我不会啊。怎么办
2 个月之前 回复
chenxu2614
chenxu2614   2017.09.11 10:29

将网络图片保存到/storage/emulated/0/Trinea/AndroidCommon/ImageCache/http___www_showfm_net_api_avatar_Jummy_jpg的时候失败

确定/storage/emulated/0/这个目录有权限访问?
这个路径是写死的还是从系统获取来的SD卡的路径?

Csdn user default icon
上传中...
上传图片
插入图片