Android - Pick and View a file of any type -
i want lets users able pick file type in phones, view again using intent.
to pick file item write ( 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) }
and show picked item:
fun showdocumentpreviewer(uri: uri) { val = intent(intent.action_view) i.data = uri mcontext.startactivity(i) }
the document picker works fine, in onactivityresult
can receive uri object of selected document, document previewer can't open object. try set mime-type previewer's intent not success. did use incorrect way open file in android? generic way show file type in android? (because want support many file types)
picked uri: content://com.android.providers.media.documents/document/image:80
update: base on @commonsware's comment, edited preview function 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 open file") }
now app crash below crash-log:
java.lang.securityexception: uid 10202 not have permission uri 0 @ content://com.android.providers.media.documents/document/audio:17915
update 2: app crash @ line mcontext.startactivity(chooser)
. here full crash-log:
fatal exception: main process: com.makeit.lite, pid: 12851 java.lang.securityexception: uid 10477 not have permission uri 0 @ content://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(native method) @ 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)
update 3: if set mimetype intent: intent.type = mimetype
, app won't crash anymore. mimetype
string picker intent (beside content:
uri). mimetype value canbe image/jpeg
or base on selected file type. although won't crash, file @ given uri won't display well. if choose gallery
intent-chooser, gallery open , show images. guess 3rd app don't know how determinate file @ given uri.
here latest source of function:
fun showattachmentpreviewer(uri: uri, mimetype: string?) { log.d("test", "preview " + uri.tostring() + " type" + mimetype) val intent = intent(intent.action_view, uri) intent.type = mimetype //can "image/jpeg" or sth corresponding 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 open file") }
did use incorrect way open file in android?
first, there may not app capable of viewing file type.
second, have not granted permission app view content. use addflags(intent.flag_grant_read_uri_permission)
on action_view
intent
.
Comments
Post a Comment