0

Hi I am developing an application in which user can download a book where i will call an URL which contains 70-80 image urls. I am using Picasso image loader to show the images in image view. All works fine but once the user download's the book i have to download all the images to SD card and I have to download the images at same time not one by one. I have searched a lot but can't find a solution. Please help me to do that.

Is there a way to save the images from server to sd card using Picasso or any other library.

Now I am Downloading all the images one by one, hence it is taking much time approx: 30min.

I have seen suggestions like using universal image loader, Picasso etc. But I want to store all the images in SD card.

Pl z suggest me some solutions. Thanks in advance.

Dhiliph Soundher
  • 159
  • 2
  • 11
  • have a look at this . http://stackoverflow.com/questions/19402818/best-way-to-save-images-which-comes-from-server-in-android – Totoro Jul 15 '14 at 12:52
  • 2
    I'm using this: [link](http://stackoverflow.com/a/13894889/2465849) in a loop to download a magazine with ~140 pages 800kb each. Starting all together it's not a problem, and its quick. – Mateusz Jablonski Jul 15 '14 at 12:56
  • Thanks for your reply Batman.....I can able to save the images in sd card but it is very slow as explained above. I want to dowmload multiple images to sd card. – Dhiliph Soundher Jul 15 '14 at 12:58
  • Thanks @Mateusz Jablonski the link was useful. – Dhiliph Soundher Jul 16 '14 at 08:04

2 Answers2

1

Two ways popped in my mind. 1- You can make a thread and assign it url of image then all of images will be downloaded together.

class SimpleThread extends Thread {
    String url;

    public SimpleThread(String url) {
        super(url);
        this.url = url;
    }

    public void run() {
        downloadBitmap(url)
    }


        /**
         * This method downloads bitmap image
         * @param url address of image
         * @return bitmap
         * @throws IOException
         */
        public Bitmap downloadBitmap(String url) throws IOException {

            this.url = url;

            HttpUriRequest request = new HttpGet(url);
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);

            StatusLine statusLine = response.getStatusLine();
            connectionStatusCode = statusLine.getStatusCode();
            if (isStatusOk(connectionStatusCode)) {
                HttpEntity entity = response.getEntity();
                byte[] bytes = EntityUtils.toByteArray(entity);

                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                return bitmap;
            } else {
                throw new IOException("Download failed, HTTP response code "
                        + connectionStatusCode + " - " + statusLine.getReasonPhrase());
            }
        }
}

2- Use Universal Image Loader instead of Picasso. I'm always using this lib.

/**
 * Created by Hesam on 15/04/14
 */
public class MyImageClient {

    private static final  String TAG = "ImageDownloader";
    public static final String DIRECTORY_NAME = "APP-NAME";

    private static MyImageClient instance = null;
    private ImageLoader imageLoader;
    private DisplayImageOptions mDisplayImageOptions;

    protected MyImageClient(Context context) {
        // Exists only to defeat instantiation.
        configImageDownloader(context);
    }

    public static MyImageClient getInstance(Context context) {
        if(instance == null) {
            instance = new MyImageClient(context);
        }

        return instance;
    }

    /**
     * This constructor will configure loader object in order to display image.
     * @param context
     */
    private void configImageDownloader(Context context) {

        File cacheDir = StorageUtils.getOwnCacheDirectory(context, DIRECTORY_NAME + "/Cache");

        // Get singleton instance of ImageLoader
        imageLoader = ImageLoader.getInstance();

        // Display options
        mDisplayImageOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .imageScaleType(ImageScaleType.EXACTLY)
                .resetViewBeforeLoading(true)
                .build();

        // Create configuration for ImageLoader (all options are optional)
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .denyCacheImageMultipleSizesInMemory()
                .threadPoolSize(4)
                .memoryCache(new WeakMemoryCache()) // LruMemoryCache(2 * 1024 * 1024)
                .memoryCacheSizePercentage(15)
                .diskCache(new UnlimitedDiscCache(cacheDir))
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                .defaultDisplayImageOptions(mDisplayImageOptions)
                .build();

        // Initialize ImageLoader with created configuration.
        imageLoader.init(config);
    }


    public void displayImage(final ImageView imageView, String imageURI) {
        if(imageView == null  ||  imageURI == null) {
            Log.e(TAG, "Either of image view or image uri is null");
            return;
        }

        // Load and display image
        imageLoader.displayImage(imageURI, imageView, mDisplayImageOptions, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String s, View view) {}

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {}

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {}

            @Override
            public void onLoadingCancelled(String s, View view) {}
        });
    }
}
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • Hesam- I have already done similar to the first way as you said, it is very slow and also tried using universal image loader still I have some performance issues. – Dhiliph Soundher Jul 16 '14 at 07:35
0

I solved the Problem using Parallel AsynTask. I have separated all the Image Url's through 4 AsynTask and then executed it using executeOnExecutor as follows.

downloadByImageLoader1 download1 = new downloadByImageLoader1();
download1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

downloadByImageLoader2 download2 = new downloadByImageLoader2();
download2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

downloadByImageLoader3 download3 = new downloadByImageLoader3();
download3.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

downloadByImageLoader4 download4 = new downloadByImageLoader4();
download4.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

And in each AsyncTask I have separated the url's and downloaded, saved to sd card using Universal Image loader as suggested in this link.

Community
  • 1
  • 1
Dhiliph Soundher
  • 159
  • 2
  • 11