1414 * limitations under the License.
1515 */
1616
17- package xyz.leezoom.tree2
17+ package moe.leer.filetree
1818
1919import android.app.Activity
20+ import android.content.ActivityNotFoundException
2021import android.content.Intent
2122import android.net.Uri
23+ import android.os.Build
24+ import android.os.StrictMode
2225import android.support.v4.app.ActivityCompat
23- import xyz.leezoom.tree2.module.FileItem
26+ import android.webkit.MimeTypeMap
27+ import android.widget.Toast
28+ import moe.leer.filetree.module.FileItem
2429import java.io.File
2530
2631
@@ -29,13 +34,31 @@ fun Activity.requestPermission(permission: String, requestCode: Int) {
2934}
3035
3136fun Activity.openFile (fileItem : FileItem ) {
32- val myIntent = Intent (Intent .ACTION_VIEW )
33- myIntent.data = Uri .fromFile(File (fileItem.absName))
34- val intent = Intent .createChooser(myIntent, " Choose an application to open with:" )
35- startActivity(intent)
37+ // exposure file uri
38+ if (Build .VERSION .SDK_INT >= 24 ) {
39+ try {
40+ val m = StrictMode ::class .java.getMethod(" disableDeathOnFileUriExposure" )
41+ m.invoke(null )
42+ } catch (e: Exception ) {
43+ e.printStackTrace()
44+ }
45+ }
46+ val file = File (fileItem.absName)
47+ val myMime = MimeTypeMap .getSingleton()
48+ val newIntent = Intent (Intent .ACTION_VIEW )
49+ val mimeType = myMime.getMimeTypeFromExtension(file.extension)
50+ newIntent.flags = (Intent .FLAG_ACTIVITY_NEW_TASK )
51+ newIntent.setDataAndType(Uri .fromFile(file), mimeType)
52+
53+
54+ try {
55+ startActivity(newIntent)
56+ } catch (e: ActivityNotFoundException ) {
57+ Toast .makeText(this , " No handler for this type of file." , Toast .LENGTH_LONG ).show()
58+ }
3659}
3760
38- fun File.isHideFile (): Boolean {
61+ fun File.isHideFile (): Boolean {
3962 return (this .name.indexOf(" ." ) == 0 )
4063}
4164
0 commit comments