gpt4 book ai didi

android - React Native - 从android内容uri获取文件名

转载 作者:行者123 更新时间:2023-12-04 15:34:35 27 4
gpt4 key购买 nike


我需要从 android 内容 uri 获取文件名,例如:
内容://com.android.providers.downloads.documents/document/15

我尝试使用 react-native-fs stat 但它返回“文件不存在”的错误。

我该怎么做?

最佳答案

我没有在 react native 中找到纯粹的解决方案,所以我在 android 中创建了 native 包并在 react native 中创建了模块,所以我可以使用它。
这是 android 代码:

@ReactMethod
public void getFileNameFromUri(String filepath, Promise promise) {
Uri uri = Uri.parse(filepath);
String fileName = "";

if (uri.getScheme().equals("content")) {
try {
Cursor cursor = reactContext.getContentResolver().query(uri, null, null, null, null);

if (cursor.moveToFirst()) {
fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Log.d("FS", "Real File name: " + fileName);
}
cursor.close();
promise.resolve(fileName);
} catch (IllegalArgumentException ignored) {
promise.reject("1", "Can not get cursor");
}
} else {
promise.resolve(fileName);
}
}

这是如何从 android 原生代码中模块化到 react native 中: react-native-modules-android

关于android - React Native - 从android内容uri获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60168797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com