GithubHelp home page GithubHelp logo

turtlebody / android-media-picker Goto Github PK

View Code? Open in Web Editor NEW
60.0 8.0 15.0 16.37 MB

A simple and easy to use Media Picker android library. Choose any image, video or audio from your device

License: MIT License

Kotlin 96.65% Java 3.35%
media-picker android-library image-picker video-picker audio-picker android picker glide

android-media-picker's Introduction

This is deprecated. Please use other library like:



API Download Android Arsenal

Demo:

Get it on Google Play

MediaPicker Library for Android (AndroidX)

A Media library for Android for selecting single/multiple media files(image/video/audio).

Setup

Step 1: Add the dependency

dependencies {
    ...
    /* media picker */
    implementation 'com.greentoad.turtlebody:media-picker:1.0.8'

    /* rxjava */
    implementation 'io.reactivex.rxjava2:rxjava:2.2.5'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
}

Usage

Step 1: Declare and Initialize MediaPicker.

Java

MediaPickerConfig pickerConfig = new MediaPickerConfig()
        .setAllowMultiSelection(false)
        .setUriPermanentAccess(true)
        .setShowConfirmationDialog(true)
        .setScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
MediaPicker.with(this,MediaPicker.MediaTypes.IMAGE)
        .setConfig(pickerConfig)
        .setFileMissingListener(new MediaPicker.MediaPickerImpl.OnMediaListener() {
            @Override
            public void onMissingFileWarning() {
                //trigger when some file are missing
            }
        })
        .onResult()
        .subscribe(new Observer<ArrayList<Uri>>() {
            @Override
            public void onSubscribe(Disposable d) { }

            @Override
            public void onNext(ArrayList<Uri> uris) {
                //uris: list of uri
            }

            @Override
            public void onError(Throwable e) { }

            @Override
            public void onComplete() { }
        });

Kotlin

val pickerConfig = MediaPickerConfig()
        .setUriPermanentAccess(false)
        .setAllowMultiSelection(allowMultiple)
        .setShowConfirmationDialog(true)
        .setScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

MediaPicker.with(this, MediaPicker.MediaTypes.IMAGE)
        .setConfig(pickerConfig)
        .setFileMissingListener(object : MediaPicker.MediaPickerImpl.OnMediaListener{
            override fun onMissingFileWarning() {
                //trigger when some file are missing
            }
        })
        .onResult()
        .subscribe({
            println ( "success: $it" )
        },{
            println ( "error: $it" )
        })

Explanation:

1. MediaPickerConfig:

It is use to set the configuration.

  1. .setAllowMultiSelection(booleanValue): tells whether to select single file or multiple file.
  2. .setUriPermanentAccess(booleanValue): grant uri access permission.
  • Temporary uri may not work once your app terminates(so storaring temporary uri in database is not good practise, so use permanent uri in such case).
  • This option only works while selecting file from default android intent.
  • The file user select from our custom ui always return uri with permanent access grant.
  1. .setShowConfirmationDialog(booleanValue): tells whether to show confirmation dialog on selecting the file(only work in single file selection).

eg.

//Pick single file with permanent access uri and confirmation dialog
MediaPickerConfig pickerConfig = new MediaPickerConfig()
        .setAllowMultiSelection(false)
        .setUriPermanentAccess(true)
        .setShowConfirmationDialog(true)
        .setScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

2. ExtraListener:

In Android many times the file not exist physically but may contain uri. Such file(uri) may produce error. So in our library we are filtering out invalid uri. So if end-developer wants to know if library filtered out uris, they can set .setFileMissingListener().

Java

.setFileMissingListener(new MediaPicker.MediaPickerImpl.OnMediaListener() {
    @Override
    public void onMissingFileWarning() {
        //trigger when some missing file are filtered out
    }
})

Kotlin

.setFileMissingListener(object : MediaPicker.MediaPickerImpl.OnMediaListener{
    override fun onMissingFileWarning() {
        //trigger when some missing file are filtered out
    }
})

3. Media types:

It's a type of file user want to select.

  1. IMAGE : for picking image files
  2. VIDEO : for picking video files
  3. AUDIO : for picking audio files

URI:

We will be returning the list of Uri after selecting the files. 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 more type of files like doc, ppt, txt etc. you can use Turtlebody Doc Picker library.

android-media-picker's People

Contributors

nirajprakash avatar vydev avatar wangsun6 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

android-media-picker's Issues

App crash when choose video from folder download

Hello admin.

App crash when choose video from folder download.

I fixed it. You can review link below.

https://github.com/coltoscosmin/FileUtils/blob/master/FileUtils.java

if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);

                if (id != null && id.startsWith("raw:")) {
                    return id.substring(4);
                }

                String[] contentUriPrefixesToTry = new String[]{
                        "content://downloads/public_downloads",
                        "content://downloads/my_downloads"
                };

                for (String contentUriPrefix : contentUriPrefixesToTry) {
                    Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
                    try {
                        String path = getDataColumn(context, contentUri, null, null);
                        if (path != null) {
                            return path;
                        }
                    } catch (Exception e) {}
                }

                // path could not be retrieved using ContentResolver, therefore copy file to accessible cache using streams
                String fileName = getFileName(context, uri);
                File cacheDir = getDocumentCacheDir(context);
                File file = generateFileName(fileName, cacheDir);
                String destinationPath = null;
                if (file != null) {
                    destinationPath = file.getAbsolutePath();
                    saveFileFromUri(context, uri, destinationPath);
                }

                return destinationPath;
            }

Return raw data from Intent

Is it possible to get data: Intent array too?
I can't get the file from the provided Uri. I want the file to upload to the server

Getting manifest merger error

Attribute provider#androidx.core.content.FileProvider@authorities value=(my_package_name) from AndroidManifest.xml:41:13-60 is also present at AndroidManifest.xml:22:13-86 value=(my_package_name.greentoad.turtlebody.mediaprovider). Suggestion: add 'tools:replace="android:authorities"' to element at AndroidManifest.xml:39:9-47:20 to override. app main manifest (this file), line 40

Media picker issues

I have a problem with a superclass used in the library. Check my log

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class com.greentoad.turtlebody.mediapicker.MediaPicker.MediaPickerImpl, unresolved supertypes: org.jetbrains.anko.AnkoLogger class com.greentoad.turtlebody.mediapicker.MediaPicker.PickerFragment, unresolved supertypes: org.jetbrains.anko.AnkoLogger > Task :app:buildInfoGeneratorDebug ย 

When opening the Picker screen the folder name screen is blank only one top Select Folder Heading and "Other" button is shown on opening

java.lang.IllegalStateException: it.getString(columnIndexFolderName) must not be null
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at com.greentoad.turtlebody.mediapicker.core.FileManager.fetchVideoFolders(FileManager.kt:107)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at com.greentoad.turtlebody.mediapicker.ui.component.folder.image_video.ImageVideoFolderFragment$fetchImageVideoFolders$bucketFetch$1.call(ImageVideoFolderFragment.kt:78)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at com.greentoad.turtlebody.mediapicker.ui.component.folder.image_video.ImageVideoFolderFragment$fetchImageVideoFolders$bucketFetch$1.call(ImageVideoFolderFragment.kt:26)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:44)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.Single.subscribe(Single.java:3603)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-07-19 23:44:28.200 27537-27537/com.service.shadecube W/System.err: at java.lang.Thread.run(Thread.java:923)

Error with android-media-picker

I cloned this project and add in android studio.
But the output was:
Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/

How can i solve this.

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.