Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法在listview项目中显示图像_Php_Android_Mysql_Listview - Fatal编程技术网

Php 无法在listview项目中显示图像

Php 无法在listview项目中显示图像,php,android,mysql,listview,Php,Android,Mysql,Listview,我正在尝试从url获取图像,并将其显示到listview项目中。 url通过php从mysql数据库获取 当前我无法获取要在我的listview项目中显示的图像 我使用的过程是首先下载图像并缓存它,然后将其显示在listview项上。(根据我在web上的搜索,我们可以获取一个直接url并将其显示到listview项目中) 任何建议或编码将是一个伟大的 下面是我正在使用的代码和面临的问题 public void GetProduct () { class GetJSON exte

我正在尝试从url获取图像,并将其显示到listview项目中。 url通过php从mysql数据库获取

当前我无法获取要在我的listview项目中显示的图像

我使用的过程是首先下载图像并缓存它,然后将其显示在listview项上。(根据我在web上的搜索,我们可以获取一个直接url并将其显示到listview项目中)

任何建议或编码将是一个伟大的

下面是我正在使用的代码和面临的问题

public void GetProduct () {

        class GetJSON extends AsyncTask<Void, Void, String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(getActivity(), "Fetching Data", "Wait...", false, false);
            }            
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;
                JSONObject jsonObject = null;
                ArrayList<HashMap<String, String>> list2= new ArrayList<HashMap<String, String>>();
                try {
                    jsonObject = new JSONObject(JSON_STRING);
                    JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
                    Log.d("JSON", Config.TAG_JSON_ARRAY);
                    for (int i = 0; i < result.length(); i++) {
                        JSONObject jo = result.getJSONObject(i);
                        String id = jo.getString(Config.TAG_ID);
                        String TITLE = jo.getString(Config.TAG_TITLE);
                        String BRAND = jo.getString(Config.TAG_BRAND);
                        String ITEMTYPE = jo.getString(Config.TAG_ITEMTYPE);
                        String BOOSTERTYPE = jo.getString(Config.TAG_BOOSTERTYPE);
                        String PRICE = jo.getString(Config.TAG_PRICE);
                        String IMGURL = jo.getString(Config.TAG_IMGURL);
                        String QTYINSTOCK = jo.getString(Config.TAG_QTYINSTOCK);
                        String QTYPENDING = jo.getString(Config.TAG_QTYPENDING);

                        HashMap<String, String> event = new HashMap<String, String>();
                        event.put(Config.TAG_ID, id);
                        event.put(Config.TAG_TITLE, TITLE);
                        event.put(Config.TAG_BRAND, BRAND);
                        event.put(Config.TAG_ITEMTYPE, ITEMTYPE);
                        event.put(Config.TAG_PRICE, PRICE);
                        event.put(Config.TAG_IMGURL, IMGURL);
                        event.put(Config.TAG_QTYINSTOCK, QTYINSTOCK);
                        event.put(Config.TAG_QTYPENDING, QTYPENDING);

//the code i added for picasso
                         ImageView img = (ImageView)getActivity().findViewById(R.id.iv_flag);
                    String imgUrl = (String)event.get(Config.TAG_IMGURL);
                    Picasso.with(getActivity()).load(imgUrl).into(img);
                            list2.add(event);
                            ListAdapter adapter = new MyAdapter(
                                            getActivity().getBaseContext(), list2, R.layout.productitem,
                                            new String[]{Config.TAG_TITLE, Config.TAG_PRICE, Config.TAG_BRAND, Config.TAG_ITEMTYPE, Config.TAG_QTYINSTOCK, "flag"},
                                            new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4, R.id.text5, R.id.iv_flag});
                            ProductListing.setAdapter(adapter);
                            HashMap<String, Object> hm = (HashMap<String, Object>)adapter.getItem(i);
                            String imgUrl = (String)event.get(Config.TAG_IMGURL);
                            ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
                            HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                            hm.put("flag_path", imgUrl);
                            hm.put("position",i);
                            imageLoaderTask.execute(hm);
                        }
                    } catch (JSONException e) {
                        Log.e("JSON Parser", "Error parsing data [" + e.getMessage() + "] " + jsonObject);
                        e.printStackTrace();
                    }
                }

                @Override
                protected String doInBackground(Void... params) {
                    RequestHandler rh = new RequestHandler();                
                    String s = rh.sendGetRequest("the url for my php file");
                    return s;
                }
            }
            final GetJSON gj = new GetJSON();
            gj.execute();
        }
        /** AsyncTask to download and load an image in ListView */
        private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{
            @Override
            protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
                InputStream iStream=null;
                String imgUrl = (String) hm[0].get("flag_path");
                Log.d("url1", imgUrl);
                int position = (Integer) hm[0].get("position");
                URL url;
                try {
                    url = new URL(imgUrl);
                    // Creating an http connection to communicate with url
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    // Connecting to url
                    urlConnection.connect();
                    // Reading data from url
                    iStream = urlConnection.getInputStream();
                    // Getting Caching directory
                    File cacheDirectory = getActivity().getBaseContext().getCacheDir();
                    // Temporary file to store the downloaded image
                    File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");
                    // The FileOutputStream to the temporary file
                    FileOutputStream fOutStream = new FileOutputStream(tmpFile);
                    // Creating a bitmap from the downloaded inputstream
                    Bitmap b = BitmapFactory.decodeStream(iStream);
                    // Writing the bitmap to the temporary file as png file
                    b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
                    // Flush the FileOutputStream
                    fOutStream.flush();
                    //Close the FileOutputStream
                    fOutStream.close();
                    // Create a hashmap object to store image path and its position in the listview
                    HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
                    // Storing the path to the temporary image file
                    hmBitmap.put("flag",tmpFile.getPath());
                    // Storing the position of the image in the listview
                    hmBitmap.put("position",position);
                    // Returning the HashMap object containing the image path and position
                    return hmBitmap;

                }catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(HashMap<String, Object> result) {
                // Getting the path to the downloaded image
                String path = (String) result.get("flag");
                // Getting the position of the downloaded image
                int position = (Integer) result.get("position");
                // Getting adapter of the listview
                SimpleAdapter adapter = (SimpleAdapter)ProductListing.getAdapter();
                // Getting the hashmap object at the specified position of the listview
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);
                // Overwriting the existing path in the adapter
                hm.put("flag",path);
                // Noticing listview about the dataset changes
                adapter.notifyDataSetChanged();
            }
        }
下面是毕加索的航海日志

FATAL EXCEPTION: main
                                                                            Process: info.androidhive.slidingmenu, PID: 1289
                                                                            java.lang.IllegalArgumentException: Target must not be null.
                                                                                at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
                                                                                at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
                                                                                at info.androidhive.slidingmenu.FindPeopleFragment$1GetJSON.onPostExecute(FindPeopleFragment.java:147)
                                                                                at info.androidhive.slidingmenu.FindPeopleFragment$1GetJSON.onPostExecute(FindPeopleFragment.java:91)
                                                                                at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                                at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                                at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:145)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5938)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

使用毕加索库从Url下载图像,它将自动从Url下载图像。要使用毕加索库,请将其添加到gradle文件中

compile 'com.squareup.picasso:picasso:2.5.2'
然后像这样做

String imgUrl = (String)event.get(Config.TAG_IMGURL);
Picasso.with(context).load(imgUrl).into("your imageview");

使用毕加索库从Url下载图像,它将自动从Url下载图像。要使用毕加索库,请将其添加到gradle文件中

compile 'com.squareup.picasso:picasso:2.5.2'
然后像这样做

String imgUrl = (String)event.get(Config.TAG_IMGURL);
Picasso.with(context).load(imgUrl).into("your imageview");

我已经在下面添加了代码,但是在毕加索的行中出现了错误。ImageView img=(ImageView)getActivity().findViewById(R.id.iv_标志);String imgUrl=(String)event.get(Config.TAG\u imgUrl);毕加索.with(getActivity()).load(imgUrl).into(img);这是正确的吗?该图像位于listview项中您会遇到什么错误以及您可以在适配器的imageview中设置图像的位置?您是指imageview用于URL中图像的布局吗?不是。我说的是Javafile,如果我错误地添加了imageview,您可以在其中初始化imageview。我应该在哪里放置它?我已经添加了代码,如前所述下面是毕加索一行的错误。ImageView img=(ImageView)getActivity().findViewById(R.id.iv_标志);String imgUrl=(String)event.get(Config.TAG\u imgUrl);毕加索.with(getActivity()).load(imgUrl).into(img);这是正确的吗?该图像位于listview项中您会遇到什么错误,以及您可以在适配器的imageview中设置图像的位置?您是指imageview用于URL图像的布局吗?不是。我说的是Javafile,如果我错误地添加了imageview,您可以在其中初始化imageview。我应该将其放置在哪里?