Example usage for android.content Intent setType

List of usage examples for android.content Intent setType

Introduction

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

Prototype

public @NonNull Intent setType(@Nullable String type) 

Source Link

Document

Set an explicit MIME data type.

Usage

From source file:Main.java

public static void sendSMS(final Context context, String message) {
    final Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.putExtra("sms_body", message);
    sendIntent.setType("vnd.android-dir/mms-sms");
    context.startActivity(sendIntent);/*from   w  w  w.  j  av  a 2  s  .  c  o m*/
}

From source file:Main.java

public static void shareOnFacebook(Context pContext, String urlToShare) {

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);

    // See if official Facebook app is found
    boolean facebookAppFound = false;
    List<ResolveInfo> matches = pContext.getPackageManager().queryIntentActivities(intent, 0);
    for (ResolveInfo info : matches) {
        if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
            intent.setPackage(info.activityInfo.packageName);
            facebookAppFound = true;//from  ww  w.  j a  va  2  s  . c  om
            break;
        }
    }

    // As fallback, launch sharer.php in a browser
    if (!facebookAppFound) {
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
    }

    pContext.startActivity(intent);
}

From source file:Main.java

public static void addToCalendar(Activity context, Long beginTime, String title) {
    ContentResolver cr = context.getContentResolver();

    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    Long time = new Date(beginTime).getTime();
    ContentUris.appendId(builder, time - 10 * 60 * 1000);
    ContentUris.appendId(builder, time + 10 * 60 * 1000);

    String[] projection = new String[] { "title", "begin" };
    Cursor cursor = cr.query(builder.build(), projection, null, null, null);

    boolean exists = false;
    if (cursor != null) {
        while (cursor.moveToNext()) {
            if ((time == cursor.getLong(1)) && title.equals(cursor.getString(0))) {
                exists = true;//w  w  w  .j a  va  2s. c  om
            }
        }
    }

    if (!exists) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", time);
        intent.putExtra("allDay", false);
        intent.putExtra("endTime", time + 60 * 60 * 1000);
        intent.putExtra("title", title);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "Event already exist!", Toast.LENGTH_LONG).show();
    }
}

From source file:Main.java

public static void sendText(Context context, String text) {
    String mimeType = getMimeTypeForExtension("txt");

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType(mimeType);
    intent.putExtra(Intent.EXTRA_TEXT, text);

    context.startActivity(intent);/*ww  w. j av a 2  s. c o  m*/
}

From source file:Main.java

public static Intent getImageFromGalleryCamera(Context context) {
    // Determine Uri of camera image to save.
    File root = new File(
            Environment.getExternalStorageDirectory() + File.separator + "dianta/camera" + File.separator);
    root.mkdirs();/*from   w  ww  .ja v a 2s.c  o m*/
    String fname = "dianta-" + System.currentTimeMillis() + ".jpg";
    File sdImageMainDirectory = new File(root, fname);
    Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

    // camera
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager lPackageManager = context.getPackageManager();
    List<ResolveInfo> listCam = lPackageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo rInfo : listCam) {
        String packageName = rInfo.activityInfo.packageName;
        Intent lIntent = new Intent(captureIntent);
        lIntent.setComponent(new ComponentName(rInfo.activityInfo.packageName, rInfo.activityInfo.name));
        lIntent.setPackage(packageName);
        //save camera result to external storage
        lIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(lIntent);
    }

    //ugly hacks for camera helper
    lastCameraImageSaved = outputFileUri;

    // gallery
    Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_PICK);

    Intent chooserIntent = Intent.createChooser(galleryIntent, "Pilih Sumber");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    return chooserIntent;
}

From source file:Main.java

public static void shareFile(Context context, String title, String filePath) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    Uri uri = Uri.parse("file://" + filePath);
    intent.setType("*/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    context.startActivity(Intent.createChooser(intent, title));
}

From source file:Main.java

/**
 * Send an email via available mail activity
 * /*from w  w  w . j  a v  a2s . c om*/
 * @param context       the app context
 * @param to            the email address send to
 * @param subject       the email subject
 * @param body          the email body
 * @param attachments   the uris for attachments
 */
public static void sendEmail(Context context, String to, String subject, String body, Uri... attachments) {
    Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);

    emailIntent.setType("plain/text");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(Intent.EXTRA_TEXT, body);

    if (attachments != null && attachments.length != 0) {
        ArrayList<Uri> uris = new ArrayList<Uri>();

        for (int i = 0; i < attachments.length; i++)
            uris.add(attachments[i]);

        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    }

    context.startActivity(Intent.createChooser(emailIntent, null));
}

From source file:Main.java

private static void shareImageOnFacebook(String imagePath, Context context) {

    Log.d("CitationsManager-ShareOnFb", "sharing the image " + imagePath);
    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    // shareIntent.putExtra(Intent.EXTRA_TEXT, "www.google.com");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
    for (final ResolveInfo app : activityList) {

        Log.d("CitationsManager-ShareOnFb", app.activityInfo.name);
        if ((app.activityInfo.name).contains("com.facebook") && !(app.activityInfo.name).contains("messenger")
                && !(app.activityInfo.name).contains("pages")) {
            final ActivityInfo activity = app.activityInfo;
            final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            shareIntent.setComponent(name);
            context.startActivity(shareIntent);
            break;
        }/*  w  ww. j av a2 s  .  c o  m*/
    }

}

From source file:Main.java

public static void addToPocket(Context context, String url, String tweetStatusId) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setPackage(PACKAGE);//from w w  w. j av a 2s. co  m
    intent.setType(MIME_TYPE);

    intent.putExtra(Intent.EXTRA_TEXT, url);
    if (tweetStatusId != null && tweetStatusId.length() > 0) {
        intent.putExtra(EXTRA_TWEET_STATUS_ID, tweetStatusId);
    }
    intent.putExtra(EXTRA_SOURCE_PACKAGE, context.getPackageName());
    context.startActivity(intent);
}

From source file:Main.java

public static Intent getShareImageIntent(Uri uri) {
    Intent image = new Intent();
    image.setAction(Intent.ACTION_SEND);
    image.putExtra(Intent.EXTRA_STREAM, uri);
    image.setType("image/*");
    return image;
}