Java tutorial
/* * Copyright (c) 2017 by Tran Le Duy * * 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. */ package com.duy.ascii.sharedcode; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; 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() + ".provider", 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")); } public static void shareText(String text, Context context) { 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(); } } }