代码之家  ›  专栏  ›  技术社区  ›  ArjunPatidar

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

  •  0
  • ArjunPatidar  · 技术社区  · 8 年前

    我为apk和链接共享创建了一个按钮链接共享工作正常,但当应用程序将共享时,apk名称将更改为“untitled”,我希望与我的apk名称相同

    我的链接代码

    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"));
            }
        });
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Jyoti JK    8 年前

    如果要共享应用程序链接和apk文件, 把它作为单独的任务来做。 (带两个按钮)

    共享链接可以像您提到的那样完成,因为它是一个文本。

    要共享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();
    }
    

    来源是 here

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

    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"));
    

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

    推荐文章