简体   繁体   English

从URL将图像加载到Android ListView中

[英]Load image into android listview from url

Hey guys i am building an app and i would like some images from url's into android imageview inside of a listview. 大家好,我正在构建一个应用程序,我希望将一些图像从url转换为listview内的android imageview。 I used the aquery library and i get this error 我使用了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)

I dont know actually how else this could have been easier. 我真的不知道这怎么可能更容易。

My code is: 我的代码是:

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;
    }
}

Could someone point me the way to load images properly?? 有人可以指出我正确加载图像的方式吗?

Thanks in advance!! 提前致谢!!

use this library form image download its very useful 用这个库表格图片下载它非常有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM