7

我使用此代码将应用程序的 APK 文件发送到另一台设备。它适用于 android 2.3.3,但不适用于 android 4+。

问题出在哪里?

我已经记录了getpackageCodePath()它,它返回了 android 4+ 上的 APK 文件,但整个代码不起作用,当蓝牙启动时,它什么也不发送。

ArrayList<Uri> uris = new ArrayList<Uri>();
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("application/vnd.android.package-archive");
uris.add(Uri.parse(getApplication().getPackageCodePath()));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, null));
4

6 回答 6

13

我使用下面的代码发送 apk。它有效

try{
    ArrayList<Uri> uris = new ArrayList<Uri>();
    Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    sendIntent.setType("application/*");
    uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir)));
    sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    startActivity(Intent.createChooser(sendIntent, null));

}catch(Exception e){}
于 2014-07-09T15:34:20.107 回答
1

换行:

uris.add(Uri.parse(getApplication().getPackageCodePath()));

uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir)));

它应该适用于所有 4.x 设备。或者,您也可以执行以下操作:

ApplicationInfo app=getPackageManager().getApplicationInfo("packageName", 0);                       
uris.add(Uri.fromFile(new File(app.publicSourceDir)));

以上两种方式都适合我。

于 2013-12-19T10:04:57.227 回答
0
InputStream in = null;
            OutputStream out = null;
            File apkPublicFilePath = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                    "myapk.apk");
            try {

                in = getResources().openRawResource(R.raw.myapk);
                out = new FileOutputStream(apkPublicFilePath);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }

                // Close the streams
                out.flush();
                out.close();
                in.close();
            } catch (IOException e) {
                Log.d(TAG, "Error in copy files");
            }

            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_STREAM,

            Uri.fromFile(apkPublicFilePath.getAbsoluteFile()));
            try {
                sendIntent.setType("application/vnd.android.package-archive");
                startActivity(Intent.createChooser(
                        sendIntent,
                        PersianReshape.reshape(getResources().getString(
                                R.string.msg_send))));
            } catch (Exception e) {
                sendIntent.setType("*/*");
                startActivity(Intent.createChooser(
                        sendIntent,
                        PersianReshape.reshape(getResources().getString(
                                R.string.msg_send))));
            }
于 2015-02-09T15:08:11.917 回答
0

您可以使用getApplicationInfo().publicSourceDir而不是getApplication().getPackageCodePath()获取应用程序 APK 的路径,然后在 aACTION_SEND Intent中使用它通过蓝牙发送文件。

在这里查看示例: Android 上的蓝牙文件传输(甚至受限类型)

于 2014-02-14T22:59:14.970 回答
0

我希望它有效:

// 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-03T16:04:21.763 回答
0
ArrayList<Uri> uris = new ArrayList<Uri>();
    Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("*/*");
uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir)));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, null));

使用/这对我有用

于 2017-06-17T07:51:46.463 回答