Example usage for android.content Intent ACTION_OPEN_DOCUMENT

List of usage examples for android.content Intent ACTION_OPEN_DOCUMENT

Introduction

In this page you can find the example usage for android.content Intent ACTION_OPEN_DOCUMENT.

Prototype

String ACTION_OPEN_DOCUMENT

To view the source code for android.content Intent ACTION_OPEN_DOCUMENT.

Click Source Link

Document

Activity Action: Allow the user to select and return one or more existing documents.

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

public void openGallery(final ActionListener response, int type) {
    if (!isGalleryTypeSupported(type)) {
        throw new IllegalArgumentException("Gallery type " + type + " not supported on this platform.");
    }/*from w w w .j  a  v  a  2s  . c o  m*/
    if (getActivity() == null) {
        throw new RuntimeException("Cannot open galery in background mode");
    }
    if (!checkForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
            "This is required to browse the photos")) {
        return;
    }
    if (editInProgress()) {
        stopEditing(true);
    }
    final boolean multi;
    switch (type) {
    case Display.GALLERY_ALL_MULTI:
        multi = true;
        type = Display.GALLERY_ALL;
        break;
    case Display.GALLERY_VIDEO_MULTI:
        multi = true;
        type = Display.GALLERY_VIDEO;
        break;
    case Display.GALLERY_IMAGE_MULTI:
        multi = true;
        type = Display.GALLERY_IMAGE;
        break;
    case -9998:
        multi = true;
        type = -9999;
        break;
    default:
        multi = false;
    }

    callback = new EventDispatcher();
    callback.addListener(response);
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    if (multi) {
        galleryIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    }
    if (type == Display.GALLERY_VIDEO) {
        galleryIntent.setType("video/*");
    } else if (type == Display.GALLERY_IMAGE) {
        galleryIntent.setType("image/*");
    } else if (type == Display.GALLERY_ALL) {
        galleryIntent.setType("image/* video/*");
    } else if (type == -9999) {
        galleryIntent = new Intent();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            galleryIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
        } else {
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
        }
        galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);

        // set MIME type for image
        galleryIntent.setType("*/*");
        galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES,
                Display.getInstance().getProperty("android.openGallery.accept", "*/*").split(","));
    } else {
        galleryIntent.setType("*/*");
    }
    this.getActivity().startActivityForResult(galleryIntent, multi ? OPEN_GALLERY_MULTI : OPEN_GALLERY);
}