Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit 6fb4969

Browse files
author
LeeReindeer
committed
[Fix] Fix open file when SDK >= 24.
1 parent bba9d6f commit 6fb4969

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

app/src/main/java/xyz/leezoom/tree2/Extend.kt renamed to app/src/main/java/moe/leer/filetree/Extend.kt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
package xyz.leezoom.tree2
17+
package moe.leer.filetree
1818

1919
import android.app.Activity
20+
import android.content.ActivityNotFoundException
2021
import android.content.Intent
2122
import android.net.Uri
23+
import android.os.Build
24+
import android.os.StrictMode
2225
import 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
2429
import java.io.File
2530

2631

@@ -29,13 +34,31 @@ fun Activity.requestPermission(permission: String, requestCode: Int) {
2934
}
3035

3136
fun 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

Comments
 (0)