- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
一开始,用户可以使用新的存储访问框架选择文件(假设应用程序是 API>19):
https://developer.android.com/guide/topics/providers/document-provider.html
然后我通过保存如下所示的 URI 来保存对那些选定文件的引用:
content://com.android.providers.downloads.documments/document/745
(在这种情况下,文件来自默认下载目录`)。
稍后,我想让用户打开这些文件(例如他们的名字显示在 UI 列表中,用户选择一个)。
我想用 Android 著名的 Intent 选择器功能来做到这一点,而我所拥有的只是上面的 URI 对象......
谢谢,
最佳答案
编辑:我修改了这个答案以包含我最初称为“编写专门的 ContentProvider”的方法示例代码。这应该完全满足问题的要求。可能会使答案太大,但它现在具有内部代码依赖性,所以让我们把它作为一个整体。要点仍然成立:如果需要,请使用下面的 ContentPrvder,但请尝试将 file://
Uris 提供给支持它们的应用程序,除非您想因某人的应用程序崩溃而受到指责。
原始答案
我会像现在这样远离存储访问框架。 Google 对它的支持不足,应用程序的支持也很糟糕,因此很难区分这些应用程序中的错误和 SAF 本身。如果您有足够的信心(这实际上意味着“可以比普通 Android 开发人员更好地使用 try-catch block ”),请自己使用存储访问框架,但仅将良好的旧 file://
路径传递给其他人.
您可以使用以下技巧从 ParcelFileDescriptor 获取文件系统路径(您可以通过调用 openFileDescriptor 从 ContentResolver 获取它):
class FdCompat {
public static String getFdPath(ParcelFileDescriptor fd) {
final String resolved;
try {
final File procfsFdFile = new File("/proc/self/fd/" + fd.getFd());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Returned name may be empty or "pipe:", "socket:", "(deleted)" etc.
resolved = Os.readlink(procfsFdFile.getAbsolutePath());
} else {
// Returned name is usually valid or empty, but may start from
// funny prefix if the file does not have a name
resolved = procfsFdFile.getCanonicalPath();
}
if (TextUtils.isEmpty(resolved) || resolved.charAt(0) != '/'
|| resolved.startsWith("/proc/") || resolved.startsWith("/fd/"))
return null;
} catch (IOException ioe) {
// This exception means, that given file DID have some name, but it is
// too long, some of symlinks in the path were broken or, most
// likely, one of it's directories is inaccessible for reading.
// Either way, it is almost certainly not a pipe.
return "";
} catch (Exception errnoe) {
// Actually ErrnoException, but base type avoids VerifyError on old versions
// This exception should be VERY rare and means, that the descriptor
// was made unavailable by some Unix magic.
return null;
}
return resolved;
}
}
您必须做好准备,上面的方法将返回 null(该文件是一个管道或套接字,这是完全合法的)或一个空路径(没有对文件父目录的读取权限)。如果发生这种情况,将整个流复制到您可以访问的某个目录。
完整的解决方案
如果您真的想坚持使用内容提供商 Uris,请继续使用。拿下面ContentProvider的代码。粘贴到您的应用中(并在 AndroidManifest 中注册)。使用下面的 getShareableUri
方法将收到的存储访问框架 Uri 转换为您自己的。将该 Uri 传递给其他应用程序而不是原始 Uri。
下面的代码是不安全的(您可以很容易地使其安全,但解释这会使这个答案的长度超出想象)。如果您愿意,请使用 file://
Uris——Linux 文件系统被广泛认为足够安全。
扩展下面的解决方案以提供没有相应 Uri 的任意文件描述符作为练习留给读者。
public class FdProvider extends ContentProvider {
private static final String ORIGINAL_URI = "o";
private static final String FD = "fd";
private static final String PATH = "p";
private static final Uri BASE_URI =
Uri.parse("content://com.example.fdhelper/");
// Create an Uri from some other Uri and (optionally) corresponding
// file descriptor (if you don't plan to close it until your process is dead).
public static Uri getShareableUri(@Nullable ParcelFileDescriptor fd,
Uri trueUri) {
String path = fd == null ? null : FdCompat.getFdPath(fd);
String uri = trueUri.toString();
Uri.Builder builder = BASE_URI.buildUpon();
if (!TextUtils.isEmpty(uri))
builder.appendQueryParameter(ORIGINAL_URI, uri);
if (fd != null && !TextUtils.isEmpty(path))
builder.appendQueryParameter(FD, String.valueOf(fd.getFd()))
.appendQueryParameter(PATH, path);
return builder.build();
}
public boolean onCreate() { return true; }
public ParcelFileDescriptor openFile(Uri uri, String mode)
throws FileNotFoundException {
String o = uri.getQueryParameter(ORIGINAL_URI);
String fd = uri.getQueryParameter(FD);
String path = uri.getQueryParameter(PATH);
if (TextUtils.isEmpty(o)) return null;
// offer the descriptor directly, if our process still has it
try {
if (!TextUtils.isEmpty(fd) && !TextUtils.isEmpty(path)) {
int intFd = Integer.parseInt(fd);
ParcelFileDescriptor desc = ParcelFileDescriptor.fromFd(intFd);
if (intFd >= 0 && path.equals(FdCompat.getFdPath(desc))) {
return desc;
}
}
} catch (RuntimeException | IOException ignore) {}
// otherwise just forward the call
try {
Uri trueUri = Uri.parse(o);
return getContext().getContentResolver()
.openFileDescriptor(trueUri, mode);
}
catch (RuntimeException ignore) {}
throw new FileNotFoundException();
}
// all other calls are forwarded the same way as above
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String o = uri.getQueryParameter(ORIGINAL_URI);
if (TextUtils.isEmpty(o)) return null;
try {
Uri trueUri = Uri.parse(o);
return getContext().getContentResolver().query(trueUri, projection,
selection, selectionArgs, sortOrder);
} catch (RuntimeException ignore) {}
return null;
}
public String getType(Uri uri) {
String o = uri.getQueryParameter(ORIGINAL_URI);
if (TextUtils.isEmpty(o)) return "*/*";
try {
Uri trueUri = Uri.parse(o);
return getContext().getContentResolver().getType(trueUri);
} catch (RuntimeException e) { return null; }
}
public Uri insert(Uri uri, ContentValues values) {
return null;
}
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) { return 0; }
}
关于Android:使用存储访问框架获得的 URI 中的 Intent 选择器打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546441/
我需要开发一个简单的网站,我通常使用 bootstrap CSS 框架,但是我想使用 Gumbyn,它允许我使用 16 列而不是 12 列。 我想知道是否: 我可以轻松地改变绿色吗? 如何使用固定布局
这个问题在这里已经有了答案: 关闭 13 年前。 与直接编写 PHP 代码相比,使用 PHP 框架有哪些优点/缺点?
我开发了一个 Spring/JPA 应用程序:服务、存储库和域层即将完成。 唯一缺少的层是网络层。我正在考虑将 Playframework 2.0 用于 Web 层,但我不确定是否可以在我的 Play
我现有的 struts Web 应用程序具有单点登录功能。然后我将使用 spring 框架创建一个不同的 Web 应用程序。然后想要使用从 struts 应用程序登录的用户来链接新的 spring 应
我首先使用Spark框架和ORMLite处理网页上表单提交的数据,在提交中文字符时看到了unicode问题。我首先想到问题可能是由于ORMLite,因为我的MySQL数据库的字符集已设置为使用utf8
我有一个使用 .Net 4.5 功能的模块,我们的应用程序也适用于 XP 用户。所以我正在考虑将这个 .net 4.5 依赖模块移动到单独的项目中。我怎样才能有一个解决方案,其中有两个项目针对不同的版
我知道这是一个非常笼统的问题,但我想我并不是真的在寻找明确的答案。作为 PHP 框架的新手,我很难理解它。 Javascript 框架,尤其是带有 UI 扩展的框架,似乎通过将 JS 代码与设计分开来
我需要收集一些关于现有 ORM 解决方案的信息。 请随意编写任何编程语言。 你能谈谈你用过的最好的 ORM 框架吗?为什么它比其他的更好? 最佳答案 我使用了 NHibernate 和 Entity
除了 Apple 的 SDK 之外,还有什么强大的 iPhone 框架可供开始开发?有没有可以加快开发时间的方法? 最佳答案 此类框架最大的是Three20 。 Facebook 和许多其他公司都使用
有人可以启发我使用 NodeJS 的 Web 框架吗?我最近开始从免费代码营学习express js,虽然一切进展顺利,但我对express到底是什么感到困惑。是全栈框架吗?纯粹是为了后端吗?我发现您
您可以推荐哪种 Ajax 框架/工具包来构建使用 struts 的 Web 应用程序的 GUI? 最佳答案 我会说你的 AJAX/javascript 库选择应该较少取决于你的后端是如何实现的,而更多
我有生成以下错误的 python 代码: objc[36554]: Class TKApplication is implemented in both /Library/Frameworks/Tk.
首先,很抱歉,如果我问的问题很明显,因为我没有编程背景,那我去吧: 我想运行一系列测试场景并在背景部分声明了几个变量(我打印它们以仔细检查它们是否已正确声明),第一个是整数,另外两个字符串为你可以看到
在我们承担的一个项目中,我们正在寻找一个视频捕获和录制库。我们的基础工作(基于 google 搜索)表明 vlc (libvlc)、ffmpeg (libavcodec) 和 gstreamer 是三
我试过没有运气的情况下寻找某种功能来杀死/中断Play中的正常工作!框架。 我想念什么吗?还是玩了!实际没有添加此功能? 最佳答案 Java stop类中没有像Thread方法那样的东西,由于种种原因
我们希望在我们的系统中保留所有重大事件的记录。例如,在数据库可能存储当前用户状态的地方,事件日志应记录对该状态的所有更改以及更改发生的时间。 事件记录工具应该尽可能接近于事件引发器的零开销,应该容纳结
那里有 ActionScript 2.0/3.0 的测试框架列表吗? 最佳答案 2010-05-18 更新 由于这篇文章有点旧,而且我刚刚收到了赞成票,因此可能值得提供一些更新的信息,这样人们就不会追
我有一个巨大的 numpy 数组列表(一维),它们是不同事件的时间序列。每个点都有一个标签,我想根据其标签对 numpy 数组进行窗口化。我的标签是 0、1 和 2。每个窗口都有一个固定的大小 M。
我是 Play 的新手!并编写了我的第一个应用程序。这个应用程序有一组它依赖的 URL,从 XML 响应中提取数据并返回有效的 URL。 此应用程序需要在不同的环境(Dev、Staging 和 Pro
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我是一名优秀的程序员,十分优秀!