VEINHORN/ScrollGalleryView

How to use list of Uri to load

malavancs opened this issue · 4 comments

Can you specify a way to load list of Uri ?

I had the same problem and what I did was create a class that implements MediaLoader. Since I did not find the issue that showed me how to do this, I'll post the code here:

public class PicassoDiskImageLoader implements MediaLoader {
    private String uri;
    private Integer thumbnailWidth;
    private Integer thumbnailHeight;

    public PicassoDiskImageLoader(String uri) {
        this.uri = uri;
    }

    public PicassoDiskImageLoader(String uri, Integer thumbnailWidth, Integer thumbnailHeight) {
        this.uri = uri;
        this.thumbnailWidth = thumbnailWidth;
        this.thumbnailHeight = thumbnailHeight;
    }

    @Override
    public boolean isImage() {
        return true;
    }

    @Override
    public void loadMedia(Context context, final ImageView imageView, final MediaLoader.SuccessCallback callback) {
        File file = new File(uri);
        Picasso.get()
                .load(file)
                .placeholder(R.drawable.placeholder_image)
                .into(imageView, new ImageCallback(callback));
    }

    @Override
    public void loadThumbnail(Context context, final ImageView thumbnailView, final MediaLoader.SuccessCallback callback) {
        thumbnailView.setContentDescription(uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf(".")));
        File file = new File(uri);
        Picasso.get()
                .load(file)
                .resize(thumbnailWidth == null ? 100 : thumbnailWidth,
                        thumbnailHeight == null ? 100 : thumbnailHeight)
                .placeholder(R.drawable.placeholder_image)
                .centerInside()
                .into(thumbnailView, new ImageCallback(callback));
    }

    private static class ImageCallback implements Callback {
        private final MediaLoader.SuccessCallback callback;

        public ImageCallback(SuccessCallback callback) {
            this.callback = callback;
        }

        @Override
        public void onSuccess() {
            callback.onSuccess();
        }

        @Override
        public void onError(Exception e) {

        }

    }
}

There is public ScrollGalleryView addMedia(List<MediaInfo> infos) method in ScrollGalleryView (you can find it here), so you can use it to add list of medias to your gallery, but you should convert list of URLs with your media to the list of MediaInfos. So like @ronanlima mentioned you can create your custom MediaLoader implementation. But much easier would be to use existing modules. For example with implementation 'com.veinhorn.scrollgalleryview:picasso-loader:1.2.1' dependency you can implement such conversion like this:

package com.veinhorn.scrollgalleryview.example.builder;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.veinhorn.scrollgalleryview.MediaInfo;
import com.veinhorn.scrollgalleryview.ScrollGalleryView;
import com.veinhorn.scrollgalleryview.builder.GallerySettings;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.veinhorn.scrollgalleryview.loader.picasso.dsl.DSL.*;

public class MainActivity extends FragmentActivity {
    private ScrollGalleryView galleryView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        galleryView = ScrollGalleryView
                .from((ScrollGalleryView) findViewById(R.id.scroll_gallery_view))
                .settings(
                        GallerySettings
                                .from(getSupportFragmentManager())
                                .thumbnailSize(100)
                                .enableZoom(true)
                                .build()
                )
                .build();

        List<String> urls = Arrays.asList(
                "http://pirate-islands.com/wp-content/uploads/2018/07/07_Dom-Fernando-II_01-636x310.jpg",
                "http://povodu.ru/wp-content/uploads/2016/04/pochemu-korabl-derzitsa-na-vode.jpg"
        );

        galleryView.addMedia(convert(urls));
    }

    private List<MediaInfo> convert(List<String> urls) {
        List<MediaInfo> medias = new ArrayList<>();
        for (String url : urls) {
            medias.add(image(url));
        }
        return medias;
    }
}

For now DSL availible only in picasso-loader module, but I'm going also implement DSL for glide-loader and fresco-loader in near future, also you can provide PRs if you want. :) DSL description could be found in MediaHelper interface which is used as spec for the DSL in modules. I'm also going to extend it with methods such as images() and videos() to make gallery initialization more flexible. Some notes about DSL can be found in #63.

I have implemented DSL spec from MediaHelper in all modules including picasso-loader, glide-loader, fresco-loader. So now you can you below code (DSL) with any of them:

private List<MediaInfo> convert(List<String> urls) {
        List<MediaInfo> medias = new ArrayList<>();
        for (String url : urls) {
            medias.add(image(url));
        }
        return medias;
    }

Starting from version 1.2.4 you can use .images(List<String> urls) and .images(String... urls) DSL methods to add list of image urls to gallery:

Using GalleryBuilder:

import static com.veinhorn.scrollgalleryview.loader.picasso.dsl.DSL.*;

galleryView = ScrollGalleryView
                .from((ScrollGalleryView) findViewById(R.id.scroll_gallery_view))
                .settings(
                        GallerySettings
                                .from(getSupportFragmentManager())
                                .thumbnailSize(100)
                                .enableZoom(true)
                                .build()
                )
                .add(images(
                        "http://pirate-islands.com/wp-content/uploads/2018/07/07_Dom-Fernando-II_01-636x310.jpg",
                        "http://povodu.ru/wp-content/uploads/2016/04/pochemu-korabl-derzitsa-na-vode.jpg"
                ))
                .add(video("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4", R.mipmap.default_video))
                .build();

Or add media to the ScrollGalleryView using .addMedia() method:

galleryView.addMedia(
                images(
                        "http://pirate-islands.com/wp-content/uploads/2018/07/07_Dom-Fernando-II_01-636x310.jpg",
                        "http://povodu.ru/wp-content/uploads/2016/04/pochemu-korabl-derzitsa-na-vode.jpg"
                )
        );

You can use any of modules: picasso-loader, fresco-loader or glide-loader.