Android原生分享与指定app分享

什么是 Android 系统的原生分享呢,如下图所示

创建一个 Intent ,指定其 Action 为 Intent.ACTION_SEND,这表示要创建一个发送指定内容的行动。

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);

 指定需要发送的内容和类型。

// 比如发送文本形式的数据内容
// 指定发送的内容
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text");
// 指定发送内容的类型
sendIntent.setType("text/plain"); 
// 比如发送二进制文件数据流内容(比如图片、视频、音频文件等等)
// 指定发送的内容 (EXTRA_STREAM 对于文件 Uri )
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
// 指定发送内容的类型 (MIME type)
shareIntent.setType("image/jpeg");

向系统发送Activity,打开系统分享选择器,出现如上图所示界面。 

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.fromFile
	if (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);

在 uni-app 中集成 Android 原生插件需要进行如下步骤: 1. 在项目根目录下创建 `nativeplugins` 目录,用于存放 Android 原生插件。 2. 在 `nativeplugins` 目录中创建插件的 Android Studio 项目,例如插件名为 `myplugin`,则创建路径为 `nativeplugins/myplugin/android/`。 3. 在 `myplugin` 的 Android 项目中,将插件打包成 `.aar` 格式的库文件。 4. 将 `.aar` 格式的库文件放到 `myplugin` 的 `libs` 目录下。 5. 在 `myplugin` 的 `build.gradle` 文件中添加如下配置: ```gradle repositories { flatDir { dirs 'libs' } } android { ... defaultConfig { ... ndk { abiFilters "armeabi-v7a", "x86" } } ... buildTypes { release { ... proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation(name:'myplugin', ext:'aar') ... } ``` 在上述配置中,`repositories` 中的 `flatDir` 配置用于让 Gradle 知道 `.aar` 文件的位置,`implementation` 配置用于将插件库文件添加到项目中。 6. 在 uni-app 项目的 `manifest.json` 文件中,将插件添加到 `app-plus -> android -> plugins` 中。例如: ```json { "app-plus": { "android": { "plugins": { "myplugin": { "version": "1.0.0", "provider": "com.example.myplugin.MyPluginProvider" } } } } } ``` 在上述配置中,`version` 用于指定插件版本号,`provider` 用于指定插件的提供者类路径。 7. 在 uni-app 项目的 `pages.json` 文件中,将插件的页面添加到 `pages` 中。例如: ```json { "pages": [ { "path": "pages/index/index", "style": { "navigationBarTitleText": "首页" } }, { "path": "pages/myplugin/myplugin", "style": { "navigationBarTitleText": "插件页面" } } ] } ``` 在上述配置中,`path` 用于指定插件页面的路径。 8. 在 uni-app 项目中,通过 `uni.requireNativePlugin('myplugin')` 来使用插件功能。 以上是集成 Android 原生插件到 uni-app 的步骤和注意事项,希望对你有帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值