Turtlebody Image Preview

Additional

Language
Kotlin
Version
N/A
Created
May 12, 2019
Updated
Aug 13, 2019
Owner
Turtle Body (Turtlebody)
Contributors
Wangsun Hakhun (wangsun6)
nirajprakash
2
Activity
Badge
Generate
Download
Source code

Advertising

Demo:

Image Preview Library for Android (AndroidX)

A Image Preview library for Android for selecting single/multiple files of any type.

Setup

Step 1: Add the dependency

dependencies {
    ...
    /*image preview*/
    implementation 'com.greentoad.turtlebody.imagepreview:image-preview:1.0.1'
}

Usage

Step 1: Declare and Initialize ImagePreview.

Java

ArrayList<Uri> arrayList = new ArrayList<>();
//add uri to arrayList

ImagePreviewConfig config = new ImagePreviewConfig().setAllowAddButton(true).setUris(arrayList);

ImagePreview.ImagePreviewImpl imagePreview = ImagePreview.with(this);

imagePreview
    .setConfig(config)
    .setListener(new ImagePreview.ImagePreviewImpl.OnImagePreviewListener() {
        @Override
        public void onDone(@NotNull ArrayList<Uri> data) {
            //after done all uri is sent back
        }

        @Override
        public void onAddBtnClicked() {
            //trigger when button clicked
        }
    })
    .start();

Kotlin

val arrayList = arrayListOf<Uri>()
//add uri to arrayList

val config = ImagePreviewConfig().setAllowAddButton(true).setUris(arrayList)

ImagePreview.with(this)
    .setConfig(config)
    .setListener(object : ImagePreview.ImagePreviewImpl.OnImagePreviewListener{
        override fun onDone(data: ArrayList<Uri>) {
            println( "uris: $data" )
        }

        override fun onAddBtnClicked() {
            printn( "addBtn clicked" )
        }
    })
    .start()

Explanation:

1. ImagePreviewConfig:

It is use to set the configuration.

  1. .setAllowAddButton(booleanValue): tells whether to show add button in preview activity.
  2. .setUris(arrayList of Uri): set array of image(uri) to be send for preview

eg.

//allow button and set uri list
ImagePreviewConfig config = new ImagePreviewConfig().setAllowAddButton(true).setUris(arrayList);

2. ExtraListener:

Call back listener when user clicked add button or done button.

eg.

.setListener(new ImagePreview.ImagePreviewImpl.OnImagePreviewListener() {
    @Override
    public void onDone(@NotNull ArrayList<Uri> data) {
        //after done all uri is sent back
    }

    @Override
    public void onAddBtnClicked() {
        //trigger when button clicked
    }
})

3. Manually dismissing ImagePreview:

ImagePreview.ImagePreviewImpl imagePreview = ImagePreview.with(this);

//note: always use same instance from which you started imagePreview
imagePreview.dismissImagePreview();

URI:

We will be returning the list of Uri after done button is clicked. That's why it is better to know about Uri first.

A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource.

In Android, Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process. You can get almost all information from uri.

URI usages:

  1. Get file from uri:
File file = new File(uri.getPath());
  1. Get mime from uri:
String mimeType = getContentResolver().getType(uri);
  1. Used in Glide:
Glide.with(context)
     .load(uri)
     .into(imageView);