0

Am trying to populate the images as background of relative layout in the list view content. here am getting the image path from the json array.so i have done this and all in the ArrayAdapter class.

Here my code which is in getView method

// Get merchant cover image path
    String coverImagePath = membershipResource.getMerCoverImagePath();
    String fullImagePath = "http://new.inspirenetz.com/in-resources/"+coverImagePath;

    // Get filename from the path
    String imageFileName = fullImagePath.substring(fullImagePath.lastIndexOf("/")+1);

    // Set merchant cover image
    Drawable drawable = imageOperations(context, fullImagePath,imageFileName);
    resizableImageView.setBackgroundDrawable(drawable);

this is what am doing for download the image

public Drawable imageOperations(Context context, String url, String saveFilename){

    try{

        InputStream is = (InputStream)this.fetch(url);
        Drawable d = Drawable.createFromStream(is, saveFilename);
        return d;

    }catch (Exception e){

        e.printStackTrace();
        return null;
    }

}

public Object fetch(String address) throws MalformedURLException,IOException {
    URL url = new URL(address);
    Object content = url.getContent();
    return content;
}

but am getting android.os.NetworkOnMainThreadException for this i have referred the following link, How to fix android.os.NetworkOnMainThreadException? but i don't know how to do this in my code

can anyone please help me. Thanks.!

Community
  • 1
  • 1
Raju
  • 1,183
  • 3
  • 11
  • 19
  • 1
    Possible duplicate of [How to fix android.os.NetworkOnMainThreadException?](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) – Akshay Bhat 'AB' Aug 29 '16 at 11:44
  • please see my comments above i have tried that but in my code where i need to do the aysn task – Raju Aug 29 '16 at 11:46
  • Call `imageOperations()` in a thread or asyncTask. – Akshay Bhat 'AB' Aug 29 '16 at 11:46
  • can u please help in answer how can i do that in my code.@Akshay Bhat – Raju Aug 29 '16 at 11:49
  • 1
    If you want to load images from network in an ArrayAdapter, it is better to use library like Picasso or Glide. Because on scroll of list, everytime you need to download images. You have to implement caching mechanism if you do this way. So Picasso/Glide handles all such things and those are easy to use. Check Picasso http://square.github.io/picasso/ – Akshay Bhat 'AB' Aug 29 '16 at 11:54
  • 1
    Hi. There's no need to re-invent the wheel. There are third party libraries which handle image operations perfectly and used by thousands of applications. Check out Nostra13 Universal Image Loader (my favorite) and Picasso. Edit: Akshay beat me to it by one minute :) – Lev Aug 29 '16 at 11:55
  • thanks for your response but more helpful if you have any link to do this – Raju Aug 29 '16 at 12:10
  • You seem to think that your problem is limited to the `NetworkOnMainThreadException`. In reality, the problem is larger, including things like row recycling, bitmap scaling, and caching. Use an [image loading library](http://android-arsenal.com/tag/46), like those suggested in earlier comments. Ideally, you use the library to solve the problem directly. Otherwise, you will need to use the library to figure out how to solve your entire set of problems with your own code. Explaining everything involved in writing image-loading code is far too long for a Stack Overflow answer. – CommonsWare Aug 29 '16 at 12:31

0 Answers0