Code Monkey home page Code Monkey logo

android-image-preview's Introduction

API Download

Demo:

Get it on Google Play

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:$latestVersion'
}

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);

Quick Links

Demos

Developers


To pick media files(audio,image,video) you can use Turtlebody Media Picker library.

android-image-preview's People

Contributors

wangsun6 avatar 73k05 avatar nirajprakash avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.