Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8728629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:41:02+00:00 2026-06-13T08:41:02+00:00

I have created a gridview application and I get the path of an image

  • 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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:41:03+00:00Added an answer on June 13, 2026 at 8:41 am
    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have created and application which contains a gridview that is populated by
I have a gridview and in that gridview i created a list of imagebuttons
I have a GridView to which I bind a dataTable that I manually created.
I have html that is dynamically created when I bind my gridview to a
In my Webforms 3.5 application, I have a GridView of users that displays the
in web application, i have a gridview in that each row contain 3 textboxes
I have created a gridview with a column of checkboxes. I want the user
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created some JQuery that will expand a div 'popup' on hover and
I have created a JSP / servlets application running in Tomcat 7. It runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.