【问题标题】:How to mimic a chooser activity/share menu?如何模仿选择器活动/共享菜单?
【发布时间】:2018-12-10 20:05:44
【问题描述】:

我尝试制作自己的选择器活动来替换 androids 共享到...弹出窗口。

我查看了ChooserActivity extends ResolverActivity 并试图复制代码。在我的清单中,我有

<intent-filter>
    <action android:name="android.intent.action.CHOOSER" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

在 onCreate 中我会这样做

Intent intent = getIntent();
Parcelable parcelable = intent.getParcelableExtra("android.intent.extra.INTENT");

Intent shareIntent = (Intent) parcelable;
shareIntent.setComponent(new ComponentName(packageName, activityName));
startActivity(shareIntent);
finish();

这有效,但带有附加文件的共享除外。在那里,我遇到了这个崩溃:

java.lang.SecurityException: UID 10098 does not have permission to content://com.android.chrome.FileProvider/images/screenshot/15305419271134395322470190280016.jpg [user 0]
    at android.os.Parcel.readException(Parcel.java:1942)
    at android.os.Parcel.readException(Parcel.java:1888)
    at android.app.IActivityManager$Stub$Proxy.startActivity(IActivityManager.java:4365)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1610)
    at android.app.Activity.startActivityForResult(Activity.java:4472)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
    at android.app.Activity.startActivityForResult(Activity.java:4430)
    at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:741)
    at android.app.Activity.startActivity(Activity.java:4791)
    at android.app.Activity.startActivity(Activity.java:4759)

我的活动似乎无法执行原始意图,因为它没有对这些文件的权限。可以做些什么来解决这个问题?有没有办法获得许可?毕竟,android 也能做到这一点。

我已经尝试了一些修复,但到目前为止没有任何效果。如果我不能让它工作,我想从 android 调用通常的共享菜单来处理这个问题,但我也不能让它工作。

addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION)

不起作用,因为这些标志已经存在于意图中。另一方面,删除它们将导致共享工作,但文件不会被共享,它将是空的。

startActivity(Intent.createChooser(shareIntent, "test");

会抛出同样的异常。

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

我试图通过为原始意图创建一个选择器来以这种方式调用 android 共享菜单,但它只是给了我一个让我选择我的应用程序的窗口。

intent.setComponent(null);
startActivity(Intent.createChooser(intent, "test"));

另一方面,这很有效,我可以选择 android 共享菜单,但是当我尝试共享时没有任何反应。该应用会表现得像没有收到任何东西一样。

intent.setComponent(new ComponentName("android", "com.android.internal.app.ChooserActivity"));
startActivity(intent);

正是我想要的,打开正常的 android 共享菜单。但是仍然存在一个问题:无论份额是多少,它都无济于事。我选择了一些东西,就是这样,它关闭了。没有其他的。为什么会这样?我基本上开始了与旧应用程序相同的意图,并将其重定向到 android。为什么它不再起作用了?我能做什么?

我读到可能需要 root 权限才能使其工作,但也许还有一种解决方法?或者至少是一种将原始意图反馈到 android 共享菜单而不破坏它的方法?

当从某些应用程序共享时,它可以工作,例如图库应用程序。那些没有问题的应用程序都使用以 content://media/... 开头的 URI。

另外,根据我的研究,它似乎可能与 FileProvider 有关,但似乎必须在共享给我的应用程序中完成,而不是我的应用程序,所以我不能做任何事情。

另外,请注意,我不是在创建共享 Intent,而是接收它并将其发送到另一个应用程序。

Here is the issuetracker from google

【问题讨论】:

    标签: android android-sharing android-intent-chooser


    【解决方案1】:

    我认为您的应用程序需要 FileProvider。 Lollipop 中的文件共享发生了变化。首先,您在 AndroidManifest.xml 中创建一个提供程序

    第一步:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mydomain.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
    </provider>
    

    第二步: 创建和定义 file_paths.xml

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <files-path name="image" path="image/"/>
        <external-path name="externalPath" path="external/path"/>
        <cache-path name="cachePath" path="./" />
    </paths>
    

    取决于您将文件放在哪里共享,您必须将它们放在正确的属性中,files-path, external-path, cache-path。更多信息请查看here

    第三步: 分享代码。

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
        File imagePath = new File(Context.getFilesDir(), "images");
        File newFile = new File(imagePath, "default_image.jpg");
        Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri);
        intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
    }else{
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
    }
    
    startActivity(intent);   
    

    【讨论】:

    • 问题是我的Chooser Activity 没有这些文件的权限,所以startActivity 会让它再次崩溃并出现SecurityException。
    • 是的。此代码用于从应用程序共享。 OP的应用是Chooser Activity,当你想分享一些东西并且需要选择一个应用时你得到的应用。
    • 我明白了,现在我明白了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2019-08-25
    • 2014-01-28
    • 1970-01-01
    相关资源
    最近更新 更多