Android - Pick And View A File Of Any Type
Solution 1:
Did I use incorrect way to open file in Android?
First, there may not be an app capable of viewing the file type.
Second, you have not granted permission for the app to view the content. Use addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
on the ACTION_VIEW
Intent
.
Solution 2:
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.type = mimeType
Change to
valintent= Intent(Intent.ACTION_VIEW);
intent.setDataAndType (uri, mimeType );
You should set uri and mime type with one statement. Otherwise the receiving app will receive null for the uri. Most apps will not tell you that they did not receive an uri. The Gallery app for instance does not.
And i do not know the exact function name for Kotlin.
Solution 3:
Fist of all, I want to say thank-you to @GreenApps, @Ankit, and @CommonsWare. Who spent time to investigate my issue.
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
when I changed to val uri = Uri.parse(uriString)
them problem solved.
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.
Here it is:
Solution 4:
If you want to perform a perfect functionality with the file you should use any third party library for this functionality.
you should check the below link for the most common libraries for the file pickers:-
Post a Comment for "Android - Pick And View A File Of Any Type"