The task is to download images from the site, create their files on the SD card and put the paths to them in the database. To call them later without an internet connection. Update data is also provided, so the image files must be overwritten. On site images in jpeg. I am trying to upload images via Picasso and output them through this library too.

Download Code:

for (k = 0; k < 5; k++) { Picasso.with(context).load(image_ar.get(k)).into(new Target() { @Override public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) { File filename = new File(context.getCacheDir().getAbsolutePath() + "/img" + k + ".jpg"); FileOutputStream out = null; try { if (filename.exists()) { if (filename.delete()) { System.out.println("Π£Π΄Π°Π»Π΅Π½ΠΎ"); } } if (filename.createNewFile()) { System.out.println("New Create"); } out = new FileOutputStream(filename); contentValues.put(DBHelperNews.KEY_HREF_TO_IMAGE, filename.getPath()); bitmap.compress(Bitmap.CompressFormat.JPEG, 40, out); } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } @Override public void onBitmapFailed(Drawable errorDrawable) { } @Override public void onPrepareLoad(Drawable placeHolderDrawable) { } }); database.insert(DBHelperNews.TABLE_NEWS, null, contentValues); } 

Output code:

  Picasso.with(context) .load("file:///" + image) .into(viewHolder.itemImage); 

In addition to the pictures, the text is also loaded. The text works fine, but the pictures are loaded through time. And once they are loaded, and with the next update, picasso does not even record. And so it is constantly, but sometimes it happens that a photo is simply not recorded, and instead of it, the previous one is displayed in the application. If this is not easier to implement through picasso, how? Thank you in advance!

    1 answer 1

    You can use Android-Universal-Image-Loader , where you specify a very large file caching time limit. Thus, if there is no picture yet - the ImageLoader comes down to the server, downloads the picture and caches it itself (you do not need to save anything to the database). Then, for the rest of your life, the ImageLoader will use the cache.

    As soon as the user clears the application / reinstalls / updates or replaces the SD card, in general, as soon as something happens to the picture, you will not have the application crash, but just the ImageLoader will go to the server again and cache it again.

    I push it, it will help you. Good luck!

    update: here is an example of creating a configuration:

     private static ImageLoaderConfiguration getImageLoaderConfiguration(Context context) { if (imageLoaderConfiguration == null) { File cacheDir = StorageUtils.getCacheDirectory(context); ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context); builder.diskCache(new LimitedAgeDiskCache(cacheDir, 60 * 60 * 24 * 14)); //врСмя ΠΊΠ΅ΡˆΠΈΡ€ΠΎΠ²Π°Π½ΠΈΡ Ρ„ΠΎΡ‚ΠΎΠ³Ρ€Π°Ρ„ΠΈΠΉ 14 Π΄Π½Π΅ΠΉ builder.diskCacheFileCount(2000); //максимальноС количСство Ρ„ΠΎΡ‚ΠΎΠ³Ρ€Π°Ρ„ΠΈΠΉ Π² кСшС builder.defaultDisplayImageOptions(getDisplayImageOptions()); imageLoaderConfiguration = builder.build(); } return imageLoaderConfiguration; } public static DisplayImageOptions getDisplayImageOptions() { if (options == null) { options = new DisplayImageOptions.Builder() .cacheInMemory(true) // default - false .cacheOnDisk(true) // default - false .imageScaleType(ImageScaleType.NONE_SAFE) // default ImageScaleType.IN_SAMPLE_POWER_OF_2 .build(); } return options; }