简体   繁体   English

在Android 4.4+中选择和加载文件

[英]Choosing and Loading Files in Android 4.4+

I am developing an app that reads it's data from several .json files on the device (or perhaps dropbox, or drive) 我正在开发一个应用程序,该设备将从设备(或也许是保管箱或驱动器)上的多个.json文件读取数据

The user selects these files using a ACTION_GET_CONTENT intent. 用户使用ACTION_GET_CONTENT目的选择这些文件。 This intent returns the URIs of the selected files, from which I obtain the paths, which are sent on to an AsyncTask to load in the background. 此意图返回所选文件的URI,我从中获取路径,然后将路径发送到AsyncTask并在后台加载。

prior to android 4.4, this worked fine. 在android 4.4之前,这可以正常工作。 Now, certain content choosers (notably android's own "downloads" activity) return uri's that do not contain the actual filename or path. 现在,某些内容选择器(尤其是android自己的“下载”活动)将返回不包含实际文件名或路径的uri。

Unfortunately AsyncTask forces you to override doInBackground(String... filenames) - which takes a series of strings as filenames. 不幸的是,AsyncTask强制您重写doInBackground(String ... filenames)-它需要一系列字符串作为文件名。 I could probably serialize the uri's somehow and pass them in the filenames parameter to the method, but now I'm thinking that obviously wasn't the intent. 我可能可以以某种方式序列化uri并将其通过filenames参数传递给该方法,但是现在我想这显然不是目的。

Therefore it is prompting me to ask the question: is there a better way to do this? 因此,这促使我提出一个问题: 是否有更好的方法来做到这一点?

What is the correct way to allow a user to choose and load a series of files in KitKat 允许用户选择和加载KitKat中的一系列文件的正确方法是什么

Instead of using AsyncTask<String, Object, Object> you can use AsyncTask<URI, Object, Object> . 可以使用AsyncTask<URI, Object, Object>代替使用AsyncTask<String, Object, Object> AsyncTask<URI, Object, Object> Which will change your doInBackground(String... args) to doInBackground(URI... args) . 这会将doInBackground(String... args)更改为doInBackground(URI... args) This will require that you use URIs instead of Strings for all file paths. 这将要求您为所有文件路径使用URI而不是字符串。

If you were to use the non-generic AsyncTask class then you must use the doInBackground(Object... args) method (not type safe). 如果要使用非通用AsyncTask类,则必须使用doInBackground(Object... args)方法(不是安全类型)。

However, using the generic AsyncTask<Params, Progress, Result> class allows you to specify the type parameters. 但是,使用通用的 AsyncTask<Params, Progress, Result>类可让您指定类型参数。 Params correlates to the varargs Type in doInBackground(Params... args) , Progress correlates to the varargs type in protected void onProgressUpdate(Progress... args) and Result correlates to the return type of protected Result doInBackground as well as the parameter type of protected void onPostExecute(Result arg) . Params相关的可变参数的类型doInBackground(Params... args)Progress相关的可变参数类型protected void onProgressUpdate(Progress... args)Result关联到的返回类型protected Result doInBackground以及参数类型protected void onPostExecute(Result arg)

In this case, it is best to use generic types as they are free of runtime errors (but not compile time). 在这种情况下,最好使用通用类型,因为它们没有运行时错误(但不是编译时)。 The same cannot be said for the non-generic AsyncTask that passes type Object references around. 对于传递类型Object引用的非泛型AsyncTask,不能说相同的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM