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 8277149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:36:11+00:00 2026-06-08T08:36:11+00:00

I am relatively new to android development and i am trying to create a

  • 0

I am relatively new to android development and i am trying to create a custom ListView with an ImageView and TextView. I am converting a URL into a bitmap image and then trying to display the image in an imageView. However I am getting an error and my application crashes immediately. The was able to display a drawable in my imageView and I made some modifications and tried to display a bitmap and then my application crashed. Any help would be greatly appreciated. Here are my 3 classes :

EDIT I used an asynctask to obtain the bitmap image on a separate thread and now my app does not crash anymore. But it does not display the listview when I start the activity. Here is my updated code.

EDIT I changed my code again and I am only passing an imageView in the execute method of my async task. I am now able to see the text in my listview, but I do not see the image. here is my edited code.

EDIT I used the debugger to find out why the bitmap wasn’t getting displayed and i found out that my bitmap variable was null. This was because I had not added internet permissions. After adding the permissions my app started to crash at the given line :

 b = BitmapFactory.decodeStream(image_url.openConnection() .getInputStream());

I had gotten a RuntimeException due to an OutofMemory error. I am not sure how to solve this. i would appreciate any help. thanks !

Here is my Loadimage AsyncTask Class:

 public class LoadImage extends AsyncTask<String, Integer, Bitmap>{

    Context callingContext = null;
    Bitmap b = null;

    ImageView view;

    public LoadImage(ImageView view){

        this.view = view;
    }

    @Override
    protected Bitmap doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String p_url = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";

        Bitmap b = null;
        try {
            URL image_url = new URL(p_url);
            b = BitmapFactory.decodeStream(image_url.openConnection() .getInputStream());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return b;
    }



    @Override
    protected void onPostExecute(Bitmap result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        view.setImageBitmap(result);

    }   
}
  • 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-08T08:36:13+00:00Added an answer on June 8, 2026 at 8:36 am

    Your code is crashing because you are loading the image file off the server on the UI thread while in strict mode. This is generally considered bad practice because if the image takes a long time to download, your app will appear to have locked up.

    Strict mode is forcing the crash (its throwing an exception). If you disable strict mode, it should let your app work fine. However the problem with doing network I/O on your UI thread remains.

    The better long term solution is to use something like an AsyncTask. This will load your image on a different thread, thus keeping your app responsive. The documentation for AsyncTask has a simple example for downloading the image, and this helper page has more information.

    For an example, I’d probably change your code to something like this (please note I’m writing this without a test run, so there maybe a few bugs):

    I’d change your List entry to be:

    public class CustomList {
    
        public String iconUrl;
        public String title;
        public CustomList(){
            super();
        }
    
        public CustomList(String icon, String title) {
            super();
            this.icon = icon;
            this.title = title;
        }
    }
    

    And then only pass in the icon URL’s to your list.

    Next create an AsyncTask:

    public class ImageDownloader extends AsyncTask<String, Integer, Bitmap> {
         public ImageDownloader(ImageView view){
              mView = view;
         }
    
         protected Bitmap doInBackground(String... url) {
            Bitmap b = null;
            try {
                URL image_url = new URL(url);
                b = BitmapFactory.decodeStream(image_url.openConnection() .getInputStream());
                list_data = new CustomList[]{
                        new CustomList(b,"Android")};
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return b;
         }
    
         protected void onPostExecute(Bitmap result) {
             mView.setImageBitmap(result);
         }
         ImageView mView;
    }
    

    And then for your Adapter, instead of setting the image, do:

    new ImageDownloader(holder.thumbnail).execute(cl.icon);
    

    That should be enough to get you started. Please note that there are a couple of issues with this sample:

    1. There is a potential memory leak. Generally speaking you should
      not hold onto reference to either Views or Activities (or other such
      objects) in an AsyncTask like I did.
    2. If you rotate the phone and
      the list changes, you might have old thumbnails show up.
    3. If a
      view is recycled, an old thumbnail might show up.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm new to android programming and i'm trying to create a relatively big 2D
I'm relatively new to android development and I'm trying to make a WebView that
I'm still relatively new to Java and Android development, so I'm still unfamiliar with
I'm relatively new to java and android development so sorry if this is a
I'm rather new to Android development, and am trying to learn with a bit
I am relatively new to Android development but I do have a pretty good
I am new to Java / Android development, and I am now trying to
I`m relatively new at Android development, although I have created a few basic applications.
I'm relatively new to Android development, so bear with me. I'm creating an app
I am relatively new to Android development. My requirement is such that I have

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.