0

I have created a gridview application and I get the path of an image with an url that is provided from json. JSON provide a lot of url paths of images, all urls has some invalid path.

How can I throw error invalid url and go to get another url?

Full Code MainActivity. CountryJSONParser

/** 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;
        }
keyser
  • 18,829
  • 16
  • 59
  • 101
kongkea
  • 9,788
  • 12
  • 39
  • 49
  • Write your own exception `InvalidURLException` and throw it when appropriate (and perhaps catch it) – keyser Oct 24 '12 at 07:12

1 Answers1

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;
    }
Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • I try to do this but it's error. look at my MainActivity at ImageLoaderTask has a lot of errors. `urlConnection` `FileOutputStream` – kongkea Oct 24 '12 at 07:19
  • Best usecase aggregate all of them separately – Yahor10 Oct 24 '12 at 07:22
  • I separated already but it has one error in `return null`. could you separate it? – kongkea Oct 24 '12 at 07:25
  • What do you mean error in return null?.Could you show more details? – Yahor10 Oct 24 '12 at 07:30
  • when I change `catch(Exception e)`to `catch(MalformedURLException e)` I have an error with `urlConnection` or `FileOutputStream` that it must surround with try catch and when i surround error with try catch, it has one error in the end of class is. `return null` eclipse said Unreachable code or one more is remove `return null` – kongkea Oct 24 '12 at 07:40
  • it doesn't work. `threadid=1: thread exiting with uncaught exception (group=0x40bdb1f8) ` – kongkea Oct 24 '12 at 07:58
  • this is the full source code http://www.mediafire.com/download.php?1hd66w9981thib9 – kongkea Oct 24 '12 at 08:06
  • sorry yesterday that I didn't see your invitation me to chat. now I online from now on. 10 Hours – kongkea Oct 25 '12 at 00:37
  • if you don't mind join this room [Android People](http://chat.stackoverflow.com/rooms/5098/android-people) I always in this room. – kongkea Oct 25 '12 at 03:17