2 freedom wings5 freedom_wings5 于 2016.03.12 20:51 提问

安卓,子线程获取网络图片,发消息给主线程,主线程用图片填充listview,出错

先上logcat:
图片说明

我的xml布局就是一个按钮,和一个listview,listview里就一个imageview,按下按钮开始从网络获取图片,然后发消息到主线程,然后listview就填充数据。xml代码就不贴了。

以下是mainactivity

 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button but;

    Bitmap bitmap;

    ListView listView;
    Bitmap mainBitmap;
    MainHandler mainHandler=new MainHandler();
    List<Map<String,Bitmap>> list=new ArrayList<>();
    Map<String,Bitmap> map;
    Message sendMsg;
    List<Bitmap> bitmapList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        but=(Button)super.findViewById(R.id.but);

        img=(ImageView)super.findViewById(R.id.img);
        listView=(ListView)super.findViewById(R.id.listview);

        but.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onClick(View v)
    {
        new Thread() {
            public void run() {

               List<String> imageList=new ArrayList<>();
                imageList.add("http://i1.hdslb.com/u_user/ed5c9f35b1311b3606c29d633793690b.png");
                imageList.add("http://i1.hdslb.com/u_user/cfdf3c751a554873ecf4687616e1f858.jpg");
                imageList.add("http://i1.hdslb.com/u_user/1462ad43f88a4b6e8b32da2447479f23.jpg");
                try
                {
                    Looper.prepare();

                    for(int i=0;i<imageList.size();i++) {
                        URL uri = new URL(imageList.get(i));
                        HttpURLConnection httpURLConnection = (HttpURLConnection) uri.openConnection();
                        httpURLConnection.setConnectTimeout(5000);
                        httpURLConnection.setUseCaches(false);
                        httpURLConnection.setDoInput(true);
                        InputStream inputStream = httpURLConnection.getInputStream();
                        bitmap = BitmapFactory.decodeStream(inputStream);
                        bitmapList.add(bitmap);
                       }


                    Message sendFinishMessage=Message.obtain();
                    sendFinishMessage.what=2;
                    mainHandler.sendMessage(sendFinishMessage);
                    Looper.loop();

                } catch (Exception e)

                {
                    Log.e("this", "获取网络图片失败" + e);
                }
            }
        }.start();


    }

    class MainHandler extends Handler
    {
        public void handleMessage(Message msg)
        {

            if(msg.what ==2)
            {
                for(int i=0;i<bitmapList.size();i++)
                {

                    map=new HashMap<>();
                    map.put("image",bitmapList.get(i));
                    list.add(map);
                }
                initListView();
            }
        }
    }

    public void initListView()
    {
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.listview,new String[]{"image"},new int[]{R.id.imageview2});
        listView.setAdapter(simpleAdapter);
    }

}

代码如上,请问是哪里出了问题?

2个回答

mofaji
mofaji   2016.03.13 16:36

看到 日志输出 说找不到文件 应该是在获取网络的图片decode 成bitmap的时候出错
URL uri = new URL(imageList.get(i));
HttpURLConnection httpURLConnection = (HttpURLConnection) uri.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
InputStream inputStream = httpURLConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
bitmapList.add(bitmap);

看来应该是这片代码的问题

lxk_1993
lxk_1993   Rxr 2016.03.18 08:36

for(int i=0;i<imageList.size();i++) {
URL uri = new URL(imageList.get(i));
HttpURLConnection httpURLConnection = (HttpURLConnection) uri.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoInput(true);
InputStream inputStream = httpURLConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
bitmapList.add(bitmap);
}

                                        循环里面加载图片错误 
                                        不是图片地址不能访问 就是 以上代码有问题  你可以参考楼上的代码
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!