Skip to content

Instantly share code, notes, and snippets.

@s1ntoneli
Last active November 9, 2018 09:08
Show Gist options
  • Save s1ntoneli/7af015b9c7c8ae35970ceedfc62275ac to your computer and use it in GitHub Desktop.
Save s1ntoneli/7af015b9c7c8ae35970ceedfc62275ac to your computer and use it in GitHub Desktop.
分享应用自己的apk
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.xxxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<root-path
name="app"
path=""/>
</paths>
public static void shareApk(Context context) {
ApplicationInfo app = context.getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.setPackage("com.android.bluetooth");
Uri uri = FileProvider.getUriForFile(context, "com.xxxx.fileprovider", new File(filePath));
intent.putExtra(Intent.EXTRA_STREAM, uri);
// intent.setData(uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooser = Intent.createChooser(intent, "Share app");
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooser);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment