How not to make delay while loading image from glide?












0















I am loading images from the server in a recyclerView and in parallel I am downloading that image too. what I actually want to do is that until image downloads till then show a blur or dark opaque preview of the image with progress bar on that.to load the image I am using glide and to download I am using Okhttp



To load the image into view:-



Glide.with(cont).load(modal.get(position).getMassege()).apply(requestOptions).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

if(modal.get(position).getProgressPercentage()==0) {
new Thread(new Runnable() {
@Override
public void run() {

downloadFile(modal.get(position).getMassege(), position, modal.get(position).getMsgid(),modal.get(position).getTimeSent());


}
}).start();
}
return false;
}
}).into(holder.image);


To download image



    public void downloadFile(String src, int position, String messageId,String timeStamp) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = connection.getInputStream();
String fileNam = src.split("/");
OutputStream output = new FileOutputStream(cont.getFilesDir() + fileNam[fileNam.length - 1]);
byte data = new byte[1024];

long total = 0;
int count = 0;
int progressPercentage = 0;
Intent intent = new Intent();
intent.setAction(Constants.INTENT_FILTER);
intent.putExtra(Constants.MESSAGE_POSITION_IN_CHAT_LIST, position);
intent.putExtra(Constants.DOWNLOADING_FILE, true);

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
progressPercentage = (int) ((total * 100) / lenghtOfFile);
if(progressPercentage==30||progressPercentage==60||progressPercentage==100) {
intent.putExtra(Constants.PROGRESS, progressPercentage);
intent.putExtra(Constants.FILE_PATH, cont.getFilesDir() + fileNam[fileNam.length - 1]);
intent.putExtra(Constants.MESSAGE_ID, messageId);
cont.sendBroadcast(intent);
}

// writing data to file
output.write(data, 0, count);
Log.d("Downloding" + progressPercentage, "Count" + count);
if (progressPercentage > 99) {
DatabaseHelperChattingToUsers databaseHelperChattingToUsers = new DatabaseHelperChattingToUsers(cont);
String userId = databaseHelperChattingToUsers.getUserExists(sendTo);
String splits = userId.split("/");
databaseHelperChattingToUsers.updateContact(new ChattingToUsers(splits[0], sendTo, timeStamp, cont.getFilesDir() + fileNam[fileNam.length - 1],"0"));

DatabaseHelper1 db = new DatabaseHelper1(cont);
db.updateContact(messageId, "", "", "", 100);
String filePath = intent.getStringExtra(Constants.FILE_PATH);
db.updateChatMessage(messageId, filePath);
}
}

} catch (IOException e) {
e.printStackTrace();

}
}


Where i have reached:



i am able to download the image in parallel while loading image in recyclerView along with progress bar showing image download with blur preview of image.



Problem i am facing:



Image takes small time while loading in recyclerView for the blur preview of image but item view is added in the recyclerView and image loads in that after sometime










share|improve this question

























  • Trying enabling the cache in the glide

    – Ümañg ßürmån
    Nov 18 '18 at 5:58











  • but on the first time it will still take time @Ümañgßürmån

    – b.k sarika
    Nov 18 '18 at 6:00













  • Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

    – Leo Pelozo
    Nov 18 '18 at 6:08











  • i have tried thumbail(0.1f) but no luck@LeoPelozo

    – b.k sarika
    Nov 18 '18 at 6:23


















0















I am loading images from the server in a recyclerView and in parallel I am downloading that image too. what I actually want to do is that until image downloads till then show a blur or dark opaque preview of the image with progress bar on that.to load the image I am using glide and to download I am using Okhttp



To load the image into view:-



Glide.with(cont).load(modal.get(position).getMassege()).apply(requestOptions).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

if(modal.get(position).getProgressPercentage()==0) {
new Thread(new Runnable() {
@Override
public void run() {

downloadFile(modal.get(position).getMassege(), position, modal.get(position).getMsgid(),modal.get(position).getTimeSent());


}
}).start();
}
return false;
}
}).into(holder.image);


To download image



    public void downloadFile(String src, int position, String messageId,String timeStamp) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = connection.getInputStream();
String fileNam = src.split("/");
OutputStream output = new FileOutputStream(cont.getFilesDir() + fileNam[fileNam.length - 1]);
byte data = new byte[1024];

long total = 0;
int count = 0;
int progressPercentage = 0;
Intent intent = new Intent();
intent.setAction(Constants.INTENT_FILTER);
intent.putExtra(Constants.MESSAGE_POSITION_IN_CHAT_LIST, position);
intent.putExtra(Constants.DOWNLOADING_FILE, true);

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
progressPercentage = (int) ((total * 100) / lenghtOfFile);
if(progressPercentage==30||progressPercentage==60||progressPercentage==100) {
intent.putExtra(Constants.PROGRESS, progressPercentage);
intent.putExtra(Constants.FILE_PATH, cont.getFilesDir() + fileNam[fileNam.length - 1]);
intent.putExtra(Constants.MESSAGE_ID, messageId);
cont.sendBroadcast(intent);
}

// writing data to file
output.write(data, 0, count);
Log.d("Downloding" + progressPercentage, "Count" + count);
if (progressPercentage > 99) {
DatabaseHelperChattingToUsers databaseHelperChattingToUsers = new DatabaseHelperChattingToUsers(cont);
String userId = databaseHelperChattingToUsers.getUserExists(sendTo);
String splits = userId.split("/");
databaseHelperChattingToUsers.updateContact(new ChattingToUsers(splits[0], sendTo, timeStamp, cont.getFilesDir() + fileNam[fileNam.length - 1],"0"));

DatabaseHelper1 db = new DatabaseHelper1(cont);
db.updateContact(messageId, "", "", "", 100);
String filePath = intent.getStringExtra(Constants.FILE_PATH);
db.updateChatMessage(messageId, filePath);
}
}

} catch (IOException e) {
e.printStackTrace();

}
}


Where i have reached:



i am able to download the image in parallel while loading image in recyclerView along with progress bar showing image download with blur preview of image.



Problem i am facing:



Image takes small time while loading in recyclerView for the blur preview of image but item view is added in the recyclerView and image loads in that after sometime










share|improve this question

























  • Trying enabling the cache in the glide

    – Ümañg ßürmån
    Nov 18 '18 at 5:58











  • but on the first time it will still take time @Ümañgßürmån

    – b.k sarika
    Nov 18 '18 at 6:00













  • Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

    – Leo Pelozo
    Nov 18 '18 at 6:08











  • i have tried thumbail(0.1f) but no luck@LeoPelozo

    – b.k sarika
    Nov 18 '18 at 6:23
















0












0








0








I am loading images from the server in a recyclerView and in parallel I am downloading that image too. what I actually want to do is that until image downloads till then show a blur or dark opaque preview of the image with progress bar on that.to load the image I am using glide and to download I am using Okhttp



To load the image into view:-



Glide.with(cont).load(modal.get(position).getMassege()).apply(requestOptions).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

if(modal.get(position).getProgressPercentage()==0) {
new Thread(new Runnable() {
@Override
public void run() {

downloadFile(modal.get(position).getMassege(), position, modal.get(position).getMsgid(),modal.get(position).getTimeSent());


}
}).start();
}
return false;
}
}).into(holder.image);


To download image



    public void downloadFile(String src, int position, String messageId,String timeStamp) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = connection.getInputStream();
String fileNam = src.split("/");
OutputStream output = new FileOutputStream(cont.getFilesDir() + fileNam[fileNam.length - 1]);
byte data = new byte[1024];

long total = 0;
int count = 0;
int progressPercentage = 0;
Intent intent = new Intent();
intent.setAction(Constants.INTENT_FILTER);
intent.putExtra(Constants.MESSAGE_POSITION_IN_CHAT_LIST, position);
intent.putExtra(Constants.DOWNLOADING_FILE, true);

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
progressPercentage = (int) ((total * 100) / lenghtOfFile);
if(progressPercentage==30||progressPercentage==60||progressPercentage==100) {
intent.putExtra(Constants.PROGRESS, progressPercentage);
intent.putExtra(Constants.FILE_PATH, cont.getFilesDir() + fileNam[fileNam.length - 1]);
intent.putExtra(Constants.MESSAGE_ID, messageId);
cont.sendBroadcast(intent);
}

// writing data to file
output.write(data, 0, count);
Log.d("Downloding" + progressPercentage, "Count" + count);
if (progressPercentage > 99) {
DatabaseHelperChattingToUsers databaseHelperChattingToUsers = new DatabaseHelperChattingToUsers(cont);
String userId = databaseHelperChattingToUsers.getUserExists(sendTo);
String splits = userId.split("/");
databaseHelperChattingToUsers.updateContact(new ChattingToUsers(splits[0], sendTo, timeStamp, cont.getFilesDir() + fileNam[fileNam.length - 1],"0"));

DatabaseHelper1 db = new DatabaseHelper1(cont);
db.updateContact(messageId, "", "", "", 100);
String filePath = intent.getStringExtra(Constants.FILE_PATH);
db.updateChatMessage(messageId, filePath);
}
}

} catch (IOException e) {
e.printStackTrace();

}
}


Where i have reached:



i am able to download the image in parallel while loading image in recyclerView along with progress bar showing image download with blur preview of image.



Problem i am facing:



Image takes small time while loading in recyclerView for the blur preview of image but item view is added in the recyclerView and image loads in that after sometime










share|improve this question
















I am loading images from the server in a recyclerView and in parallel I am downloading that image too. what I actually want to do is that until image downloads till then show a blur or dark opaque preview of the image with progress bar on that.to load the image I am using glide and to download I am using Okhttp



To load the image into view:-



Glide.with(cont).load(modal.get(position).getMassege()).apply(requestOptions).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
return false;
}

@Override
public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

if(modal.get(position).getProgressPercentage()==0) {
new Thread(new Runnable() {
@Override
public void run() {

downloadFile(modal.get(position).getMassege(), position, modal.get(position).getMsgid(),modal.get(position).getTimeSent());


}
}).start();
}
return false;
}
}).into(holder.image);


To download image



    public void downloadFile(String src, int position, String messageId,String timeStamp) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
int lenghtOfFile = connection.getContentLength();
InputStream input = connection.getInputStream();
String fileNam = src.split("/");
OutputStream output = new FileOutputStream(cont.getFilesDir() + fileNam[fileNam.length - 1]);
byte data = new byte[1024];

long total = 0;
int count = 0;
int progressPercentage = 0;
Intent intent = new Intent();
intent.setAction(Constants.INTENT_FILTER);
intent.putExtra(Constants.MESSAGE_POSITION_IN_CHAT_LIST, position);
intent.putExtra(Constants.DOWNLOADING_FILE, true);

while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
progressPercentage = (int) ((total * 100) / lenghtOfFile);
if(progressPercentage==30||progressPercentage==60||progressPercentage==100) {
intent.putExtra(Constants.PROGRESS, progressPercentage);
intent.putExtra(Constants.FILE_PATH, cont.getFilesDir() + fileNam[fileNam.length - 1]);
intent.putExtra(Constants.MESSAGE_ID, messageId);
cont.sendBroadcast(intent);
}

// writing data to file
output.write(data, 0, count);
Log.d("Downloding" + progressPercentage, "Count" + count);
if (progressPercentage > 99) {
DatabaseHelperChattingToUsers databaseHelperChattingToUsers = new DatabaseHelperChattingToUsers(cont);
String userId = databaseHelperChattingToUsers.getUserExists(sendTo);
String splits = userId.split("/");
databaseHelperChattingToUsers.updateContact(new ChattingToUsers(splits[0], sendTo, timeStamp, cont.getFilesDir() + fileNam[fileNam.length - 1],"0"));

DatabaseHelper1 db = new DatabaseHelper1(cont);
db.updateContact(messageId, "", "", "", 100);
String filePath = intent.getStringExtra(Constants.FILE_PATH);
db.updateChatMessage(messageId, filePath);
}
}

} catch (IOException e) {
e.printStackTrace();

}
}


Where i have reached:



i am able to download the image in parallel while loading image in recyclerView along with progress bar showing image download with blur preview of image.



Problem i am facing:



Image takes small time while loading in recyclerView for the blur preview of image but item view is added in the recyclerView and image loads in that after sometime







java android android-recyclerview android-glide glide






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 9:08









Fantômas

32.4k156388




32.4k156388










asked Nov 18 '18 at 5:28









b.k sarikab.k sarika

12




12













  • Trying enabling the cache in the glide

    – Ümañg ßürmån
    Nov 18 '18 at 5:58











  • but on the first time it will still take time @Ümañgßürmån

    – b.k sarika
    Nov 18 '18 at 6:00













  • Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

    – Leo Pelozo
    Nov 18 '18 at 6:08











  • i have tried thumbail(0.1f) but no luck@LeoPelozo

    – b.k sarika
    Nov 18 '18 at 6:23





















  • Trying enabling the cache in the glide

    – Ümañg ßürmån
    Nov 18 '18 at 5:58











  • but on the first time it will still take time @Ümañgßürmån

    – b.k sarika
    Nov 18 '18 at 6:00













  • Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

    – Leo Pelozo
    Nov 18 '18 at 6:08











  • i have tried thumbail(0.1f) but no luck@LeoPelozo

    – b.k sarika
    Nov 18 '18 at 6:23



















Trying enabling the cache in the glide

– Ümañg ßürmån
Nov 18 '18 at 5:58





Trying enabling the cache in the glide

– Ümañg ßürmån
Nov 18 '18 at 5:58













but on the first time it will still take time @Ümañgßürmån

– b.k sarika
Nov 18 '18 at 6:00







but on the first time it will still take time @Ümañgßürmån

– b.k sarika
Nov 18 '18 at 6:00















Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

– Leo Pelozo
Nov 18 '18 at 6:08





Check out Glide.Thumnail() bumptech.github.io/glide/doc/options.html#thumbnail-requests

– Leo Pelozo
Nov 18 '18 at 6:08













i have tried thumbail(0.1f) but no luck@LeoPelozo

– b.k sarika
Nov 18 '18 at 6:23







i have tried thumbail(0.1f) but no luck@LeoPelozo

– b.k sarika
Nov 18 '18 at 6:23














1 Answer
1






active

oldest

votes


















0














Glide would download image for you, why are you taking extra efforts by downloading it via okHttp ?



There will be always delay in downloading a complete image. It depends on your network speed and image size.



To reduce a delay you can have different sizes of images stored on server. On client side download small size image first and display it in recyclerview. ( Smaller image size == less time to download.)



Upon clicking row in recyclerview download the complete image by hitting another urls.



e.g.



https://www.example.com/images/small/image1.png ( 250x250 px = 100kb )



https://www.example.com/images/medium/image1.png ( 1000x1000 px = 400kb )



https://www.example.com/images/large/image1.png (3000x3000 ox = 1200kb)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358172%2fhow-not-to-make-delay-while-loading-image-from-glide%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Glide would download image for you, why are you taking extra efforts by downloading it via okHttp ?



    There will be always delay in downloading a complete image. It depends on your network speed and image size.



    To reduce a delay you can have different sizes of images stored on server. On client side download small size image first and display it in recyclerview. ( Smaller image size == less time to download.)



    Upon clicking row in recyclerview download the complete image by hitting another urls.



    e.g.



    https://www.example.com/images/small/image1.png ( 250x250 px = 100kb )



    https://www.example.com/images/medium/image1.png ( 1000x1000 px = 400kb )



    https://www.example.com/images/large/image1.png (3000x3000 ox = 1200kb)






    share|improve this answer




























      0














      Glide would download image for you, why are you taking extra efforts by downloading it via okHttp ?



      There will be always delay in downloading a complete image. It depends on your network speed and image size.



      To reduce a delay you can have different sizes of images stored on server. On client side download small size image first and display it in recyclerview. ( Smaller image size == less time to download.)



      Upon clicking row in recyclerview download the complete image by hitting another urls.



      e.g.



      https://www.example.com/images/small/image1.png ( 250x250 px = 100kb )



      https://www.example.com/images/medium/image1.png ( 1000x1000 px = 400kb )



      https://www.example.com/images/large/image1.png (3000x3000 ox = 1200kb)






      share|improve this answer


























        0












        0








        0







        Glide would download image for you, why are you taking extra efforts by downloading it via okHttp ?



        There will be always delay in downloading a complete image. It depends on your network speed and image size.



        To reduce a delay you can have different sizes of images stored on server. On client side download small size image first and display it in recyclerview. ( Smaller image size == less time to download.)



        Upon clicking row in recyclerview download the complete image by hitting another urls.



        e.g.



        https://www.example.com/images/small/image1.png ( 250x250 px = 100kb )



        https://www.example.com/images/medium/image1.png ( 1000x1000 px = 400kb )



        https://www.example.com/images/large/image1.png (3000x3000 ox = 1200kb)






        share|improve this answer













        Glide would download image for you, why are you taking extra efforts by downloading it via okHttp ?



        There will be always delay in downloading a complete image. It depends on your network speed and image size.



        To reduce a delay you can have different sizes of images stored on server. On client side download small size image first and display it in recyclerview. ( Smaller image size == less time to download.)



        Upon clicking row in recyclerview download the complete image by hitting another urls.



        e.g.



        https://www.example.com/images/small/image1.png ( 250x250 px = 100kb )



        https://www.example.com/images/medium/image1.png ( 1000x1000 px = 400kb )



        https://www.example.com/images/large/image1.png (3000x3000 ox = 1200kb)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 13 '18 at 7:15









        Nilesh DeokarNilesh Deokar

        2,0291637




        2,0291637






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358172%2fhow-not-to-make-delay-while-loading-image-from-glide%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word