0

我已经创建了一个 gridview 应用程序,并且我使用从 json 提供的 url 获取图像的路径。JSON提供了很多图片的url路径,所有的url都有一些无效的路径。

如何抛出错误无效 url 并去获取另一个 url?

完整代码MainActivityCountryJSON解析器

/** 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 frameUrl;
            imgUrl = (String) hm[0].get("photo_path");
            frameUrl = (String) hm[0].get("frame_path");
            int position = (Integer) hm[0].get("position");

            URL url;
            URL urlFrame;
            try {
                url = new URL(imgUrl);
                urlFrame = new URL(frameUrl);
                // 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 = 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("photo",tmpFile.getPath());
                hmBitmap.put("frame", 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;
        }
4

1 回答 1

0
InputStream iStream = null;
    String imgUrl;
    String frameUrl;
    imgUrl = (String) hm[0].get("photo_path");
    frameUrl = (String) hm[0].get("frame_path");
    int position = (Integer) hm[0].get("position");

    URL url;
    URL urlFrame;
    try {
        url = new URL(imgUrl);
        urlFrame = new URL(frameUrl);
        // 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 = 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("photo", tmpFile.getPath());
        hmBitmap.put("frame", 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 (MalformedURLException e) {
        // TODO Auto-generated catch block
        return null;
    } catch (FileNotFoundException e) {
        return null;
    } catch (IOException e) {
        return null;
    }
于 2012-10-24T07:13:18.183 回答