Pick photo from gallery The content uri will be saved in the intent in onActivityResult - Android Intent

Android examples for Intent:System Gallery

Description

Pick photo from gallery The content uri will be saved in the intent in onActivityResult

Demo Code


//package com.java2s;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;

public class Main {
    /**//w ww .j a  v  a  2  s  .c  o  m
     * Request code for sdk version lower than 19
     */
    public static final int PICK_PHOTO = 0;
    /**
     * Request code for sdk version higher than 19(including 19)
     */
    public static final int PICK_PHOTO_KITKAT = 1;

    /**
     * Pick photo from gallery<p/>
     * The content uri will be saved in the intent in onActivityResult
     *
     * @param context Context
     */
    public static void pickPhotoFromGallery(Context context) {
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                ((Activity) context).startActivityForResult(intent,
                        PICK_PHOTO_KITKAT);
            } else {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
                intent.setType("image/*");
                ((Activity) context).startActivityForResult(intent,
                        PICK_PHOTO);
            }
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials