从url将图像加载到android listview中

时间:2014-04-09 22:03:44

标签: android json listview

嘿伙计们我正在构建一个应用程序,我想从url的一些图像进入listview内的android imageview。我使用了aquery库,我收到了这个错误

04-09 17:56:43.864: I/System.out(1580): resolveUri failed on bad bitmap uri: 
04-09 17:56:43.894: E/BitmapFactory(1580): Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)

我实际上并不知道这怎么可能更容易。

我的代码是:

public class JsonReadTask extends AsyncTask<String, Void, String> {
        public JsonReadTask(){
            super();
        }
        @SuppressLint("InlinedApi")
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this, ProgressDialog.THEME_DEVICE_DEFAULT_DARK);
            pDialog.setTitle(R.string.waiting);
            pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            pDialog.setMessage(getString(R.string.get_stocks));
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.setInverseBackgroundForced(true);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            }

            catch (Exception e) {
                Intent intent1 = new Intent(MainActivity.this, RefreshActivity.class);
                startActivityForResult(intent1, 0);
            } 
            return null;
        }
        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }
            catch (Exception e) {
                Intent intent1 = new Intent(MainActivity.this, RefreshActivity.class);
                startActivity(intent1);
                startActivityForResult(intent1, 0);
            }
            return answer;
        }
        @Override
        protected void onPostExecute(String result) {
            ListDrawer();
            pDialog.dismiss();

        }
    }// end async task
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[] { url });

    }
    public void ListDrawer() {
        stocksList = new ArrayList<HashMap<String, String>>();
        androidAQuery = new AQuery(MainActivity.this);
        try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes");
            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("name");
                String price = jsonChildNode.optString("price");
                String price1 = jsonChildNode.optString("price1");
                String price2 = jsonChildNode.optString("price2");
                String price3 = jsonChildNode.optString("price3");
                String price4 = jsonChildNode.optString("price4");
                String price5 = jsonChildNode.optString("price5");
                String price6 = jsonChildNode.optString("price6");
                String price7 = jsonChildNode.optString("price7");
                String price8 = jsonChildNode.optString("price8");
                String price9 = jsonChildNode.optString("price9");
                String price10 = jsonChildNode.optString("price10");
                String price11 = jsonChildNode.optString("price11");
                String price12 = jsonChildNode.optString("price12");
                String price13 = jsonChildNode.optString("price13");
                String price14 = jsonChildNode.optString("price14");
                String price15 = jsonChildNode.optString("price15");
                image = jsonChildNode.optString("image");
                ImageView imagelist = (ImageView)findViewById(R.id.imagestartinglist);
                androidAQuery.id(imagelist).image(image, true, true, 72, R.drawable.ic_image_preloaded);
                stocksList.add(createStockList(name, price, price1, price2, price3, price4, price5, price6, price7, price8, price9, price10, price11, price12, price13, price14, price15));
            }
        } catch (Exception e) {
            //Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                    //Toast.LENGTH_SHORT).show();
            Intent intent1 = new Intent(MainActivity.this, RefreshActivity.class);
            startActivityForResult(intent1, 0);
        } 
        ImageView imagelist = (ImageView)findViewById(R.id.imagestartinglist);

        String[] from = { "name", "price", "androidAQuery.id(imagelist).image(image, true, true, 72, R.drawable.ic_image_preloaded)"};
        int[] to = { R.id.stock_name, R.id.stock_price, R.id.imagestartinglist};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, stocksList,
                R.layout.list_item,
                from, to);
        listView.setAdapter(simpleAdapter);
    }
    public HashMap<String, String> createStockList(String name, String price, String price1, String price2, String price3, String price4, String price5, String price6, String price7, String price8, String price9, String price10, String price11, String price12, String price13, String price14, String price15) {
        HashMap<String, String> stockNameNo = new HashMap<String, String>();
        stockNameNo.put("name", name);
        stockNameNo.put("price", price);
        stockNameNo.put("price1", price1);
        stockNameNo.put("price2", price2);
        stockNameNo.put("price3", price3);
        stockNameNo.put("price4", price4);
        stockNameNo.put("price5", price5);
        stockNameNo.put("price6", price6);
        stockNameNo.put("price7", price7);
        stockNameNo.put("price8", price8);
        stockNameNo.put("price9", price9);
        stockNameNo.put("price10", price10);
        stockNameNo.put("price11", price11);
        stockNameNo.put("price12", price12);
        stockNameNo.put("price13", price13);
        stockNameNo.put("price14", price14);
        stockNameNo.put("price15", price15);
        //stockNameNo.put("image", image);
        return stockNameNo;
    }
}

有人能指出我正确加载图片的方法吗?

提前致谢!!

1 个答案:

答案 0 :(得分:1)

使用此库表单下载图片非常有用

  

https://github.com/nostra13/Android-Universal-Image-Loader

相关问题