How to add .apk file inside intent

2020-06-24 15:50发布

I want to add .apk file inside intent. I want to create a button "Share" which will share the whole app through bluetooth or any other application which have capability to send application. If this can be done by other way then please tell me! Thanks

5条回答
Emotional °昔
2楼-- · 2020-06-24 16:18

Binary data is shared using the ACTION_SEND action combined with setting the appropriate MIME type and placing the URI to the data in an extra named EXTRA_STREAM. This is commonly used to share an image but can be used to share any type of binary content

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

For details; see this: Send binary data

查看更多
贪生不怕死
3楼-- · 2020-06-24 16:21
 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();
    }

}
查看更多
孤傲高冷的网名
4楼-- · 2020-06-24 16:26

use this if you want to send (share) .apk file inside app using Bluetooth

// 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"));
查看更多
Explosion°爆炸
5楼-- · 2020-06-24 16:27

You can add the location of the APK of your app

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

note that you might get SecurityException if the file is not accessible to your app or the sharing app

查看更多
相关推荐>>
6楼-- · 2020-06-24 16:27
 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();
        }
    }
查看更多
登录 后发表回答