Giter VIP home page Giter VIP logo

photo-gallery's Introduction

Photo-Gallery

  • Shows photo gallery similar to as in whatsapp, folders segregated.
  • Would work perfect on Scoped storage android 10 and above.
  • Is meant for image picking use in flutter, adding instructions provided below.

Instructions to add in flutter project for image-picking alternative

  • Copy paste all 5 files from this project into your flutter android module at app/src/main/java in a package 'ImageHelperFiles'
  • Don't forget to copy paste required resource files, drawables and layouts.
  • Register activities in manifest as follows.
<activity
            android:name=".ImageHelperFiles.GalleryImages"
            android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity
            android:name=".ImageHelperFiles.PhotoGallery"
            android:theme="@style/Theme.AppCompat.NoActionBar" />
  • In your flutter android module at app/src/main/java in MainActivity.java class, paste the following code to register the method call.
private MethodChannel.Result mResult;
    private final int IMAGE_PICKER = 100;

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        String CHANNEL = "METHOD-CHANNEL-NAME";
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
                .setMethodCallHandler((call, result) -> {
                    switch (call.method) {
                        case "pick_image" :
                            mResult = result;
                            pickFromGallery();
                            break;
                        default:
                            result.notImplemented();
                    }
                });
    }

    void pickFromGallery() {
        Intent galleryIntent = new Intent(this, GalleryImages.class);
        startActivityForResult(galleryIntent, IMAGE_PICKER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==IMAGE_PICKER) {
            switch(resultCode) {
                case RESULT_OK:
                    ArrayList<String> selectedImages = data.getStringArrayListExtra("selected_images");
                    mResult.success(selectedImages);
                    break;
                case RESULT_CANCELED:
                    selectedImages = new ArrayList<>();
                    mResult.success(selectedImages);
                    break;
                default:
                    mResult.error("Unknown Error", "Unhandled error occurred!", null);
            }
        }
    }
  • Place this file in lib folder of your flutter project.
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class ImagePicker {

  final MethodChannel channel;
  LeimoImagePicker({@required this.channel});

  Future<List> chooseImages() async {
    return await channel.invokeMethod("pick_image");
  }

}
  • Handle onClick function on OK Button in GalleryImages.java file to setup logic for sending back selected images. Suggested way is shown below.
Intent intent = getIntent();
intent.putStringArrayListExtra("selected_images", selectedImages);
setResult(RESULT_OK, intent);
finish();
  • You can also handle onBackPressed method as follows.
Intent intent = getIntent();
setResult(RESULT_CANCELED, intent);
finish();
  • To trigger the method for image picking, add the following code in your flutter project.
ImagePicker imagePicker = ImagePicker(
            channel: const MethodChannel("METHOD-CHANNEL-NAME"));
        var _images = await imagePicker.chooseImages();

photo-gallery's People

Contributors

kartik-pant-23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ghoedev

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.