3

我想在意图中添加 .apk 文件。我想创建一个“共享”按钮,它将通过蓝牙或任何其他能够发送应用程序的应用程序共享整个应用程序。如果这可以通过其他方式完成,请告诉我!谢谢

4

5 回答 5

2
 List(ApplicationInfo) mAppList=getPackageManager().getInstalledApplications(0);        
ApplicationInfo item = mAppList.get(position);



 public static void ShareAPK(ApplicationInfo item,Context ctx) {
    try {
        File srcFile = new File(item.publicSourceDir);
        Intent share = new Intent();
        share.setAction(Intent.ACTION_SEND);
        share.setType("application/vnd.android.package-archive");
        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
        ctx.startActivity(Intent.createChooser(share, "Sharing"));
    } catch (Exception e) {
        e.printtrace();
    }

}
于 2015-03-31T05:43:11.823 回答
1

使用 ACTION_SEND 操作共享二进制数据,同时设置适当的 MIME 类型并将数据的 URI 放在一个名为 EXTRA_STREAM 的额外文件中。这通常用于共享图像,但可用于共享任何类型的二进制内容

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("application/vnd.android.package-archive");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

详情;看到这个:发送二进制数据

于 2014-07-26T08:34:17.297 回答
0
 public void shareAPKMine()
    {
        try {

            PackageManager m = getPackageManager();
            String s = getPackageName();
            PackageInfo p = m.getPackageInfo(s, 0);
            s = p.applicationInfo.sourceDir;
            File sourceLocation = new File(s);
            String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/shefi/"+"test.apk";
            File targetLocation = new File(path);
            Utils.copyDirectory(sourceLocation, targetLocation);
            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("application/vnd.android.package-archive");
            shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            Uri uri = Uri.parse(path);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(shareIntent, "Share"+"  "+getResources().getString(R.string.app_name)));

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

 // If targetLocation does not exist, it will be created.
    public static void copyDirectory(File sourceLocation , File targetLocation)
            throws IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists() && !targetLocation.mkdirs()) {
                throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
            }

            String[] children = sourceLocation.list();
            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {

            // make sure the directory we plan to store the recording in exists
            File directory = targetLocation.getParentFile();
            if (directory != null && !directory.exists() && !directory.mkdirs()) {
                throw new IOException("Cannot create dir " + directory.getAbsolutePath());
            }

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);

            }
            in.close();
            out.close();
        }
    }
于 2015-03-04T02:45:38.843 回答
0

您可以添加应用程序 APK 的位置

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("*/*");
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        Uri uri = Uri.parse("/data/apps/"+getApplicationContext().getPackageName()+".apk");
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(shareIntent, "Share via"));

请注意,SecurityException如果您的应用或共享应用无法访问该文件,您可能会收到

于 2014-07-20T17:08:08.823 回答
0

如果您想使用蓝牙在应用程序内发送(共享).apk 文件,请使用此功能

// Get current ApplicationInfo to find .apk path
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;

Intent intent = new Intent(Intent.ACTION_SEND);

// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");

// Only use Bluetooth to send .apk
intent.setPackage("com.android.bluetooth");

// Append file and send Intent
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(intent, "Share app"));
于 2015-05-03T15:34:44.017 回答