Android-选择并查看任何类型的文件
我想让用户能够选择手机中的任何文件类型,然后使用intent再次查看.
I want to lets users able to pick any file type in their phones, then view it again using intent.
要选择文件项,我要这样写(我使用的是Kotlin,Java中的同样问题):
To pick file item I write this ( I'm using Kotlin, same issue in Java):
fun goToDocumentPicker() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "*/*"
if (intent.resolveActivity(mContext.packageManager) != null)
mContext.startActivityForResult(intent, REQUEST_DOCUMENT)
}
并显示选择的项目:
fun showDocumentPreviewer(uri: Uri) {
val i = Intent(Intent.ACTION_VIEW)
i.data = uri
mContext.startActivity(i)
}
文档选择器工作正常,在onActivityResult
中,我可以接收所选文档的Uri对象,但是文档预览器无法打开该对象.已经尝试将mime-type设置为预览器的意图,但未成功.我是否使用错误的方式在Android中打开文件?有什么通用的方法可以显示Android中的任何文件类型? (因为我想支持许多文件类型)
The document picker works fine, in the onActivityResult
I can receive the Uri object of selected document, but the document previewer can't open the object. Already try to set mime-type to the previewer's intent but not success. Did I use incorrect way to open file in Android? Any generic way to show any file type in Android? (Because I want to support many file types)
选择了Uri:content://com.android.providers.media.documents/document/image:80
更新: 基于@CommonsWare的评论,我编辑了预览功能,如下所示:
Update: Base on @CommonsWare's comment, I edited the preview function like below:
fun showAttachmentPreviewer(uri: Uri, mimeType: String?) {
Log.d("TEST", "Preview " + uri.toString())
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(intent, "Open with")
if (intent.resolveActivity(mContext.packageManager) != null)
mContext.startActivity(chooser)
else
mContext.showSnackBar("No suitable application to open file")
}
现在,应用程序始终崩溃,并显示以下崩溃日志:
Now the app always crash with below crash-log:
java.lang.SecurityException:Uid 10202没有uri的权限 0 @ content://com.android.providers.media.documents/document/audio:17915
java.lang.SecurityException: Uid 10202 does not have permission to uri 0 @ content://com.android.providers.media.documents/document/audio:17915
更新2:
我的应用程序在mContext.startActivity(chooser)
行崩溃.这是完整的崩溃日志:
Update 2:
My app crash at line mContext.startActivity(chooser)
. Here is the full crash-log:
致命异常:主要 流程:com.makeit.lite,PID:12851 java.lang.SecurityException:Uid 10477没有对uri的许可 0 @ 内容://com.android.providers.media.documents/document/image:24776 在android.os.Parcel.readException(Parcel.java:1540) 在android.os.Parcel.readException(Parcel.java:1493) 在 android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2514) 在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1494) 在android.app.Activity.startActivityForResult(Activity.java:3913) 在 android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 在 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 在android.app.Activity.startActivityForResult(Activity.java:3860) 在 android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 在android.app.Activity.startActivity(Activity.java:4184) 在android.app.Activity.startActivity(Activity.java:4152) 在 com.makeit.lite.attachment.AttachmentNavigator.showAttachmentPreviewer(AttachmentNavigator.kt:92) 在 com.makeit.lite.attachment.list.AttachmentListPresenter.onAttachmentClicked(AttachmentListPresenter.kt:37) 在 com.makeit.lite.attachment.list.AttachmentListFragment $ onViewCreated $ 1.onItemClick(AttachmentListFragment.kt:39) 在 eu.davidea.viewholders.FlexibleViewHolder.onClick(FlexibleViewHolder.java:121) 在android.view.View.performClick(View.java:5156) 在android.view.View $ PerformClick.run(View.java:20755) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:145) 在android.app.ActivityThread.main(ActivityThread.java:5835) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
FATAL EXCEPTION: main Process: com.makeit.lite, PID: 12851 java.lang.SecurityException: Uid 10477 does not have permission to uri 0 @ content://com.android.providers.media.documents/document/image:24776 at android.os.Parcel.readException(Parcel.java:1540) at android.os.Parcel.readException(Parcel.java:1493) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2514) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1494) at android.app.Activity.startActivityForResult(Activity.java:3913) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) at android.app.Activity.startActivityForResult(Activity.java:3860) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) at android.app.Activity.startActivity(Activity.java:4184) at android.app.Activity.startActivity(Activity.java:4152) at com.makeit.lite.attachment.AttachmentNavigator.showAttachmentPreviewer(AttachmentNavigator.kt:92) at com.makeit.lite.attachment.list.AttachmentListPresenter.onAttachmentClicked(AttachmentListPresenter.kt:37) at com.makeit.lite.attachment.list.AttachmentListFragment$onViewCreated$1.onItemClick(AttachmentListFragment.kt:39) at eu.davidea.viewholders.FlexibleViewHolder.onClick(FlexibleViewHolder.java:121) at android.view.View.performClick(View.java:5156) at android.view.View$PerformClick.run(View.java:20755) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5835) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
更新3:
如果将mimeType设置为intent:intent.type = mimeType
,该应用程序将不再崩溃. mimeType
是我从选择器意图中获得的字符串(在content:
uri旁边). mimeType值可以为image/jpeg
或基于所选文件类型的任何值.尽管不会崩溃,但给定uri的文件也不会显示.如果我从Intent-Chooser中选择Gallery
,则图库"将打开并显示所有图像.我猜第三个应用程序不知道如何在给定的uri上确定文件.
Update 3:
If I set mimeType to intent: intent.type = mimeType
and the app won't crash anymore. The mimeType
is the string I get from the picker intent (beside the content:
uri). The mimeType value canbe image/jpeg
or anything base on selected file type. Although it won't crash, the file at given uri won't display as well. If I choose the Gallery
from Intent-Chooser, the Gallery open and show all images. I guess the 3rd app don't know how to determinate the file at given uri.
这是我功能的最新来源:
Here is the latest source of my function:
fun showAttachmentPreviewer(uri: Uri, mimeType: String?) {
Log.d("TEST", "Preview " + uri.toString() + " For type" + mimeType)
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.type = mimeType //Can be "image/jpeg" or sth corresponding to the filetype.
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooser = Intent.createChooser(intent, "Open with")
if (intent.resolveActivity(mContext.packageManager) != null)
mContext.startActivity(chooser)
else
mContext.showSnackBar("No suitable application to open file")
}
我首先要对@ GreenApps,@ Ankit和@CommonsWare表示感谢.谁花时间调查我的问题.
Fist of all, I want to say thank-you to @GreenApps, @Ankit, and @CommonsWare. Who spent time to investigate my issue.
我终于找到了将源代码提取到分离的简单项目中的根本原因.这是因为我以这种方式将uri-string解析为uri-instance(我的问题中没有包含root-cause的代码.我很抱歉):
I finally found the root cause when extracting the source to separated simple project. It's because I parse uri-string into uri-instance this way (the code of root-cause didn't included in my question. My apologies):
val uri = Uri.parse(URLDecoder.decode(uriString, "UTF-8")) as Uri
当我更改为val uri = Uri.parse(uriString)
时,问题已解决.
when I changed to val uri = Uri.parse(uriString)
them problem solved.
顺便说一句,我想在我的Github中共享用于选择器和预览器的示例代码,因为有人可能需要看看. Java源位于master
分支上,而Kotlin源位于kotlin-version
分支上.现在,使用此简单代码,我可以选择任何文件类型(图像,音频,视频,pdf,xlsx等),然后稍后通过Intent.ACTION_VIEW将其打开.
Btw, I want to share the sample code for picker and previewer in my Github for someone may need to take a look. The java source is on master
branch, and the Kotlin source is on kotlin-version
branch. Using this simple code, now I can pick any file type (image, audio, video, pdf, xlsx,...) and then open it later via Intent.ACTION_VIEW.
这里是: