Skip to content Skip to sidebar Skip to footer

Images Loading Very Slowly

I am fetching data from movie database API and sometimes images do not load and sometimes it loads slowly. I am decoding images url to bitmap and then setting them to image view us

Solution 1:

You are using InputStreamReader, which i think isn't necessary, It may as well be the reason why it is slow. I can't post this as a comment so I have to post as answer.

Try this code for doInBackground instead of using InputStreamReader.

//Call your methodnewFetchData().execute("http://api.themoviedb.org/3/movie/popular?api_key=b6f6fcfbb225d8c500e4404655ccadcc&certification=G");


publicclassFetchDataextendsAsyncTask<String, Void, Void> {

        @OverrideprotectedBooleandoInBackground(String... urls) {
            try {
                HttpParams httpParameters = newBasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
                HttpConnectionParams.setSoTimeout(httpParameters, 5000);

                //------------------>>HttpGet httppost = newHttpGet(urls[0]);
                HttpClient httpclient = newDefaultHttpClient(httpParameters);
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);


                    JSONObject jsono = newJSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("results");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObjectobject = jarray.getJSONObject(i);

                        //Get your data here Example : poster_pathobject.getString("poster_path");

                    }

                    returntrue;
                }

                //------------------>>

            } catch (ConnectTimeoutException e) {
                Log.e("Timeout Exception: ", e.toString());
            } catch (SocketTimeoutException ste) {
                Log.e("Timeout Exception: ", ste.toString());
            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            returnfalse;
        }
    }

And you need to import this classes

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;

Solution 2:

Use Library Such as Glide Or Picasso.!

    @Override
    publicvoidonBindViewHolder(Holder holder, int position) {
       // holder.imageView.setImageBitmap(Data.bImage[position]);


   Glide.with(ctx)
                    .load(imageDownloadUrl)
                    .into(holder.imageView);

 Picasso.with(context).load("").placeholder(Data.bImage[position]).into(holder.imageView);

or//R.drawable.drawableName
Picasso.with(context).load(Data.bImage[position]).into(holder.imageView);    

}

       compile 'com.squareup.picasso:picasso:2.5.2'

      compile 'com.github.bumptech.glide:glide:3.6.1'

        compile 'com.android.support:support-v4:25.1.0'

Post a Comment for "Images Loading Very Slowly"