当用户想要打开 pdf 时,我想使用我的应用程序“打开”,因此我在清单 xml 中添加了意图过滤器,并创建了
+native-intent.ts
来获取 FileProvider 中的 pdf 路径。从“我的文件”打开时它可以工作,“我的文件”是 Android 中默认的本机文件管理器。
但是当我从其他应用程序(例如 Kakaotalk 或 Slack)打开文件并将该
content://
文件复制到缓存目录中的 file://
时,会出现以下错误。
我试图让原生模块来调用
currentActivity.grantUriPermission(currentActivity.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
,但它不起作用(java.lang.SecurityException:UID 10874没有内容权限)
发生错误的代码
async function copySharedPathToUri(contentUri: string) {
const destinationUri = FileSystem.cacheDirectory + "tempFile";
await FileSystem.copyAsync({
from: contentUri,
to: destinationUri,
});
return destinationUri;
}
错误
Error uploading file: [Error: Call to function 'ExponentFileSystem.copyAsync' has been rejected.
→ Caused by: java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.kakao.talk.FileProvider/external_files/emulated/0/Android/data/com.kakao.talk/contents/MTg=/2412.10468v1.pdf from pid=25200, uid=10858 requires the provider be exported, or grantUriPermission()]
+native-intent.ts
export function redirectSystemPath({
path,
initial,
}: {
path: string;
initial: string;
}) {
try {
console.log("redirectSystemPath", path, initial);
return `/?sharedPath=${encodeURIComponent(path)}`;
} catch {
return "/";
}
}
这些是依赖项
"dependencies": {
"@expo-google-fonts/inter": "^0.2.3",
"@expo/vector-icons": "^14.0.2",
"@react-native-async-storage/async-storage": "1.23.1",
"@react-navigation/native": "^7.0.0",
"axios": "^1.7.7",
"cookie": "^1.0.1",
"date-fns": "^4.1.0",
"expo": "~52.0.19",
"expo-apple-authentication": "~7.1.2",
"expo-build-properties": "~0.13.1",
"expo-constants": "~17.0.3",
"expo-dev-client": "~5.0.6",
"expo-document-picker": "~13.0.1",
"expo-file-system": "~18.0.6",
"expo-font": "~13.0.1",
"expo-image": "~2.0.3",
"expo-linking": "~7.0.3",
"expo-router": "~4.0.13",
"expo-share-intent": "^3.1.1",
"expo-splash-screen": "~0.29.18",
"expo-status-bar": "~2.0.0",
"expo-system-ui": "~4.0.6",
"expo-web-browser": "~14.0.1",
"nativewind": "^4.1.23",
"patch-package": "^8.0.0",
"react": "18.3.1",
"react-dom": "18.3.1",
"react-native": "0.76.5",
"react-native-gesture-handler": "~2.20.2",
"react-native-reanimated": "~3.16.1",
"react-native-safe-area-context": "4.12.0",
"react-native-screens": "^4.0.0",
"react-native-web": "~0.19.10",
"react-native-webview": "13.12.5",
"tailwindcss": "^3.4.14"
},
这是 xml 的一部分
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="moonlight"/>
<data android:scheme="com.corca.moonlight"/>
<data android:scheme="exp+moonlight"/>
</intent-filter>
已经花费了超过24小时,请帮助我:)
也许你可以尝试请求许可
const downloadsUri = FileSystem.StorageAccessFramework.getUriForDirectoryInRoot('你需要的目录')
const permissions =
await FileSystem.StorageAccessFramework.requestDirectoryPermissionsAsync(
downloadsUri
)