在 android 中共享应用程序 link 和 apk 文件

Share application link and apk file in android

我为 apk 和 link share 创建了一个按钮 link share 工作正常但是当应用程序将被共享时,apk 名称更改为 "untitled" 我想要相同的名称作为我的 apk 名称

我的代码 link

btnShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           ApplicationInfo app = getApplicationContext().getApplicationInfo();
            String filePath = app.publicSourceDir;
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            Uri uri = Uri.parse(filePath);
            sharingIntent.setType("*/*");
            sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
            sharingIntent.putExtra(Intent.EXTRA_TEXT, "Click to blue link and download thegame. https://drive.google.com/open?id=10Nc5BoYn4NZ_O8ae32UVQwyzdCzFxNy");
            startActivity(Intent.createChooser(sharingIntent, "Share app using"));
        }
    });

如果您想分享您的应用 link 和您的 apk 文件,将其作为单独的任务进行。(有两个按钮)

分享link可以按照你说的方式完成,因为它是文本。

要共享 apk 文件,首先您需要获取您的应用程序 apk 文件

 ApplicationInfo packageinfo = context.getPackageManager().getApplicationInfo(yourpackagename, 0);
 File file = new File(packageinfo.publicSourceDir);

然后将文件复制到另一个

try {
    //Make new directory in new location
    File tempFile = new File(getExternalCacheDir() + "/ExtractedApk");
    //If directory doesn't exists create new
    if (!tempFile.isDirectory())
        if (!tempFile.mkdirs())
            return;
    //Get application's name and convert to lowercase
    tempFile = new File(tempFile.getPath() + "/" + "appname" + ".apk");
    //If file doesn't exists create new
    if (!tempFile.exists()) {
        if (!tempFile.createNewFile()) {
            return;
        }
    }
    //Copy file to new location
    InputStream in = new FileInputStream(originalApk);
    OutputStream out = new FileOutputStream(tempFile);

    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    System.out.println("File copied.");
    //Open share dialog
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
    startActivity(Intent.createChooser(intent, "Share app via"));

} catch (IOException e) {
    e.printStackTrace();
}

来源是

您可以使用创建的文件路径

分享它
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri uri = Uri.parse(filepath);
sharingIntent.setType("*/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Click to blue link and download thegame. https://drive.google.com/open?id=10Nc5BoYn4NZ_O8ae32UVQwyzdCzFxNy");
startActivity(Intent.createChooser(sharingIntent, "Share app using"));

这将根据所选应用程序将内容更改为 text/apk