Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2015 Marten Gajda <marten@dmfs.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import android.app.Activity;

import android.content.Intent;

import android.provider.MediaStore;

public class Main {
    /**
     * calls the android chooser to select content for attachment applications immediately. Override {@link Activity#onActivityResult()} in your activity to get
     * the result of the chooser and forward it to {@link #startAttachmentActivityFromChooserResult(Intent, Activity, int)} to call for attachment applications.
     * The given <b>requestCode</b> indicates the chooser result.
     * 
     * @param activity
     *            Android {@link Activity} {@link Context}
     * @param requestCode
     *            this code will be returned in {@link Activity#onActivityResult()}
     */
    public static void startChooserForAttachmentActivity(Activity activity, int requestCode) {
        activity.startActivityForResult(getChooserIntent(), requestCode);
    }

    /**
     * Returns an intent calling the android chooser. The chooser will provide apps, which listen to one of the following actions
     * {@link Intent#ACTION_GET_CONTENT} , {@link MediaStore#ACTION_IMAGE_CAPTURE}, {@link MediaStore#ACTION_VIDEO_CAPTURE},
     * {@link MediaStore.Audio.Media#RECORD_SOUND_ACTION}
     * 
     * @return Intent that opens the app chooser.
     */
    public static Intent getChooserIntent() {
        // GET_CONTENT Apps
        Intent getContentIntent = new Intent();
        getContentIntent.setAction(Intent.ACTION_GET_CONTENT);
        getContentIntent.setType("*/*");
        getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);
        // ACTION_IMAGE_CAPTURE Apps
        Intent captureImageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // ACTION_VIDEO_CAPTURE Apps
        Intent captureVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        // RECORD_SOUND_ACTION Apps
        Intent recordSoungIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
        Intent finalIntent = Intent.createChooser(getContentIntent, "test");
        finalIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                new Intent[] { captureImageIntent, captureVideoIntent, recordSoungIntent });
        return finalIntent;
    }
}