通过intent共享应用程序,但它不起作用

kognpnkq  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(407)

我正在制作聊天应用程序,并希望通过意向分享应用程序。解决这个问题的办法不多请帮忙。这是我的代码,但它在启动活动中抛出错误

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){

                case R.id.share :

                    ApplicationInfo api = getApplicationContext().getApplicationInfo();
                    String apkPatch = api.sourceDir;

                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("application/vnd.android.package-archive");
                    intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(apkPatch)));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    startActivity(Intent.createChooser(intent, "ShareVia"));

                    Toast.makeText(NavigationActivity.this, "shareVia", Toast.LENGTH_SHORT).show();
                case R.id.aboutus :
                    Toast.makeText(getApplicationContext(), "aboutUs", Toast.LENGTH_SHORT).show();

            }

            return true;
        }
    });

这是运行屏幕中的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chatbox, PID: 19897
android.os.FileUriExposedException: file:///data/app/com.example.chatbox-7bck8l1ys2PwFlDmiMFq2w%3D%3D/base.apk exposed beyond app through ClipData.Item.getUri()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:2083)
    at android.net.Uri.checkFileUriExposed(Uri.java:2388)
    at android.content.ClipData.prepareToLeaveProcess(ClipData.java:977)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10809)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10815)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10794)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1710)
    at android.app.Activity.startActivityForResult(Activity.java:5331)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
    at android.app.Activity.startActivityForResult(Activity.java:5262)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:662)
    at android.app.Activity.startActivity(Activity.java:5660)
    at android.app.Activity.startActivity(Activity.java:5628)
    at com.example.chatbox.NavigationActivity$1.onNavigationItemSelected(NavigationActivity.java:121)
    at com.google.android.material.navigation.NavigationView$1.onMenuItemSelected(NavigationView.java:170)
    at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
    at androidx.appcompat.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:91)
    at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
    at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
    at com.google.android.material.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:352)
    at android.view.View.performClick(View.java:7257)
    at android.view.View.performClickInternal(View.java:7213)
    at android.view.View.access$3800(View.java:828)
    at android.view.View$PerformClick.run(View.java:27921)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7830)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)

i/进程:发送信号。pid:19897信号:9

qcuzuvrc

qcuzuvrc1#

问题是,对于新的api,文件uri方案与旧api的方案不同。所以使用 new File(apkPatch) 将为较新的API引发此异常。
所以,你可以把绳子传给 Uri.parse ,而不是 File 为了解决这个问题而不是使用 Uri.fromFile 因此,请替换以下代码行:

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(apkPatch)));

使用:

intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(apkPatch));

我已经尝试过你的解决方案,它的工作非常好,但apk文件名是base.apk,但我想在那里我的应用程序名(聊天框)而不是基地
现在要更改apk文件名,请在下面添加到intent

intent.putExtra(Intent.EXTRA_SUBJECT, "Chat Box.apk");

相关问题