/* * Copyright (C) 2017-2018 Tran Le Duy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.duy.common.utils; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.content.FileProvider; import android.widget.Toast; import java.io.File; /** * Created by Duy on 09-Aug-17. */ public class ShareUtil { public static void shareImage(Context context, File file) { Uri uri; if (Build.VERSION.SDK_INT >= 24) { uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", file); } else { uri = Uri.fromFile(file); } Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, uri); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setType("image/*"); context.startActivity(Intent.createChooser(intent, "Share image via")); } @Deprecated public static void shareText(String text, Context context) { shareText(context, text); } public static void shareText(Context context, CharSequence text) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, text); intent.setType("text/plain"); context.startActivity(intent); } public static void shareMessenger(String text, Context context) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, text); sendIntent.setType("text/plain"); sendIntent.setPackage("com.facebook.orca"); try { context.startActivity(sendIntent); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(context, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show(); } } public static void shareApp(Activity context, String appId) { try { Intent intent = new Intent(Intent.ACTION_SEND); String url = String.format("http://play.google.com/store/apps/details?id=%s", appId); intent.putExtra(Intent.EXTRA_TEXT, url); intent.setType("text/plain"); context.startActivity(intent); } catch (ActivityNotFoundException e) { } } public static void shareThisApp(@NonNull Activity context) { shareApp(context, context.getPackageName()); } }