skip to Main Content

I have a recyclerview which I populate from an API , there is a URL of a picture I want to show in my imageview, I tried few methods it gives Network Thread exception,

My Adapter Class,

 @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ItemPayload model = list.get(position);
    Log.d("aaa",model.getTrack().getName().toString());

    String image = model.getTrack().getAlbum().getImages().get(position).getUrl();
    URL newurl = null;
    try {
        newurl = new URL(image);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    Bitmap mIcon_val = null;
    try {
        HttpURLConnection conn= (HttpURLConnection)newurl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        int length = conn.getContentLength();
        int[] bitmapData =new int[length];
        byte[] bitmapData2 =new byte[length];
        InputStream is = conn.getInputStream();
        BitmapFactory.Options options = new BitmapFactory.Options();

        mIcon_val = BitmapFactory.decodeStream(is,null,options);

        holder.songCover.setImageBitmap(mIcon_val);
        
    } catch (IOException e) {
        e.printStackTrace();
    }



    holder.textSong.setText(model.getTrack().getName());
    holder.textArtist.setText(model.getTrack().getArtists().get(0).getName());
    holder.textTrack.setText(model.getTrack().getAlbum().getTotal_tracks().toString());
    String url = model.getTrack().getExternal_urls().getSpotify();
    holder.textSong.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent viewIntent =
                    new Intent("android.intent.action.VIEW", Uri.parse(url));
            viewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(viewIntent);
        }
    });

}

Does Anyone know a easier way to do it? or any fixes?

2

Answers


  1. Loading images using HttpURLConnection load on main thread and stops the processing of the app or causes ANR. It’s recommended you load images in background thread.
    Glide library is image loading library which does the tasks easily for you.

    Use glide to load the images

      implementation 'com.github.bumptech.glide:glide:4.12.0'
    

    Sample Code

     Glide
        .with(myFragment)
        .load(url)
        .centerCrop()
        .placeholder(R.drawable.loading_spinner)
        .into(myImageView);
    
    Login or Signup to reply.
  2. You can use Picasso library for the same.

    Permission in android manifest

    <uses-permission android:name="android.permission.INTERNET" />
    

    Add Picasso in app level build.gradle

    implementation 'com.squareup.picasso:picasso:2.5.2'
    

    Loading image

    Picasso.get().load(model.getTrack().getAlbum().getImages().get(position).getUrl()).into(holder.songCover);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search