哪個(gè)網(wǎng)站可以免費(fèi)看小說不收費(fèi)百度網(wǎng)絡(luò)營(yíng)銷推廣
什么是 Android 系統(tǒng)的原生分享呢,如下圖所示
創(chuàng)建一個(gè) Intent ,指定其 Action 為 Intent.ACTION_SEND,這表示要?jiǎng)?chuàng)建一個(gè)發(fā)送指定內(nèi)容的行動(dòng)。
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
?指定需要發(fā)送的內(nèi)容和類型。
// 比如發(fā)送文本形式的數(shù)據(jù)內(nèi)容
// 指定發(fā)送的內(nèi)容
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text");
// 指定發(fā)送內(nèi)容的類型
sendIntent.setType("text/plain");
// 比如發(fā)送二進(jìn)制文件數(shù)據(jù)流內(nèi)容(比如圖片、視頻、音頻文件等等)
// 指定發(fā)送的內(nèi)容 (EXTRA_STREAM 對(duì)于文件 Uri )
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// 指定發(fā)送內(nèi)容的類型 (MIME type)
shareIntent.setType("image/jpeg");
向系統(tǒng)發(fā)送Activity,打開系統(tǒng)分享選擇器,出現(xiàn)如上圖所示界面。?
startActivity(Intent.createChooser(shareIntent, "Share"));
完整代碼如下:
// 原生通用分享文本
public void shareText(String title, String text){Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);if (title.isEmpty()){title = "share";}sendIntent.putExtra(Intent.EXTRA_TEXT, text);sendIntent.setType("text/plain");startActivityForResult(Intent.createChooser(sendIntent, title), 80001);
}// 原生通用分享圖片
public void shareImage(String title, String filePath){Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);if (title.isEmpty()){title = "share";}File file = new File(filePath);Uri uri = getFileUri(this, file);sendIntent.putExtra(Intent.EXTRA_STREAM, uri);sendIntent.setType("image/png");startActivityForResult(Intent.createChooser(sendIntent, title), 80002);
}public Uri getFileUri(Context context, File file){Uri uri;// 低版本直接用 Uri.fromFileif (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {uri = Uri.fromFile(file);}else {try {uri = FileProvider.getUriForFile(this, getPackageName0() + ".fileProvider", file);} catch (Exception e) {e.printStackTrace();uri = getImageContentUri(context, file);}if (uri == null) {uri = getImageContentUri(context, file);}}return uri;
}public Uri getImageContentUri(Context context, File imageFile) {String filePath = imageFile.getAbsolutePath();Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",new String[] { filePath }, null);if (cursor != null && cursor.moveToFirst()) {int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));Uri baseUri = Uri.parse("content://media/external/images/media");return Uri.withAppendedPath(baseUri, "" + id);} else {if (imageFile.exists()) {ContentValues values = new ContentValues();values.put(MediaStore.Images.Media.DATA, filePath);return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);} else {return null;}}
}
如果要分享到指定的app呢?
// 特定App 分享文本
public void shareTextByApp(String pkgName, String appName, String title, String text){if (!checkAppInstalled(this, pkgName)){Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();return;}if (title.isEmpty()){title = "share";}Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);sendIntent.putExtra(Intent.EXTRA_TEXT, text);sendIntent.setType("text/plain");sendIntent.setPackage(pkgName);startActivity(sendIntent);
}// 特定App 分享文本
public void shareImageByApp(String pkgName, String appName, String title, String filePath){if (!checkAppInstalled(this, pkgName)){Toast.makeText(getApplicationContext(), "You need install the " + appName + " first", Toast.LENGTH_LONG).show();return;}if (title.isEmpty()){title = "share";}Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);File file = new File(filePath);Uri uri = getFileUri(this, file);sendIntent.putExtra(Intent.EXTRA_STREAM, uri);sendIntent.setType("image/png");sendIntent.setPackage(pkgName);startActivity(sendIntent);
}// 是否安裝某app
public boolean checkAppInstalled(Context context, String pkgName) {try {context.getPackageManager().getPackageInfo(pkgName, 0);} catch (Exception x) {return false;}return true;
}
//例如 WhatsApp 的分享
shareTextByApp("com.whatsapp", "WhatsApp", title, content);//Facebook 的分享
shareImageByApp("com.facebook.katana", "Facebook", title, filePath);