@@ -3,6 +3,7 @@ package com.github.code.gambit.utility
3
3
import android.content.ContentUris
4
4
import android.content.Context
5
5
import android.database.Cursor
6
+ import android.database.CursorIndexOutOfBoundsException
6
7
import android.net.Uri
7
8
import android.provider.DocumentsContract
8
9
import android.provider.MediaStore
@@ -15,37 +16,61 @@ object FileUtil {
15
16
fun Uri.isSchemeTypeContent (): Boolean = " content" .equals(this .scheme!! , ignoreCase = true )
16
17
17
18
fun getPathFromLocalUri (context : Context , uri : Uri ): String? {
19
+ val path: String? = try {
20
+ _getPathFromLocalUri (context, uri)
21
+ } catch (exp: CursorIndexOutOfBoundsException ) {
22
+ exp.printStackTrace()
23
+ uri.path
24
+ } catch (exp: NullPointerException ) {
25
+ exp.printStackTrace()
26
+ uri.path
27
+ } catch (exp: NumberFormatException ) {
28
+ exp.printStackTrace()
29
+ uri.path
30
+ }
31
+ return path?.let {
32
+ if (File (it).exists()) {
33
+ path
34
+ } else {
35
+ null
36
+ }
37
+ }
38
+ }
39
+
40
+ fun _getPathFromLocalUri (context : Context , uri : Uri ): String? {
18
41
// DocumentProvider
19
42
when {
20
43
DocumentsContract .isDocumentUri(context, uri) -> {
21
44
// ExternalStorageProvider
22
45
when {
23
46
isExternalStorageDocument(uri) -> {
24
47
val docId = DocumentsContract .getDocumentId(uri)
25
- val split = docId.split(" :" .toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
48
+ val split =
49
+ docId.split(" :" .toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
26
50
val type = split[0 ]
27
51
28
52
// This is for checking Main Memory
29
- return if (" primary" .equals(type, ignoreCase = true )) {
53
+ if (" primary" .equals(type, ignoreCase = true )) {
30
54
if (split.size > 1 ) {
31
- context.getExternalFilesDir(null ).toString() + " /" + split[1 ]
55
+ return context.getExternalFilesDir(null ).toString() + " /" + split[1 ]
32
56
} else {
33
- context.getExternalFilesDir(null ).toString() + " /"
57
+ return context.getExternalFilesDir(null ).toString() + " /"
34
58
}
35
59
// This is for checking SD Card
36
- } else {
60
+ } /* else {
37
61
val path = "storage" + "/" + docId.replace(":", "/")
38
62
if (File(path).exists()) {
39
63
path
40
64
} else {
41
65
"/storage/sdcard/" + split[1]
42
66
}
43
- }
67
+ }*/
44
68
}
45
69
isDownloadsDocument(uri) -> {
46
70
val fileName = getFilePath(context, uri)
47
71
if (fileName != null ) {
48
- val path = context.getExternalFilesDir(null ).toString() + " /Download/" + fileName
72
+ val path = context.getExternalFilesDir(null )
73
+ .toString() + " /Download/" + fileName
49
74
if (File (path).exists()) {
50
75
return path
51
76
}
@@ -56,30 +81,18 @@ object FileUtil {
56
81
id = id.split(" :" )[1 ]
57
82
}
58
83
val contentUri = ContentUris .withAppendedId(
59
- Uri .parse(" content://downloads/public_downloads" ), java.lang.Long .valueOf(id)
84
+ Uri .parse(" content://downloads/public_downloads" ),
85
+ java.lang.Long .valueOf(id)
60
86
)
61
87
return getDataColumn(context, contentUri, null , null )
62
88
}
63
89
isMediaDocument(uri) -> {
64
90
val docId = DocumentsContract .getDocumentId(uri)
65
- val split = docId.split(" :" .toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
91
+ val split =
92
+ docId.split(" :" .toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
66
93
val type = split[0 ]
67
94
68
- var contentUri: Uri ? = null
69
- when (type) {
70
- " image" -> {
71
- contentUri = MediaStore .Images .Media .EXTERNAL_CONTENT_URI
72
- }
73
- " video" -> {
74
- contentUri = MediaStore .Video .Media .EXTERNAL_CONTENT_URI
75
- }
76
- " audio" -> {
77
- contentUri = MediaStore .Audio .Media .EXTERNAL_CONTENT_URI
78
- }
79
- " document" -> {
80
- contentUri = Uri .parse(" content://media/external/documents/media" )
81
- }
82
- }
95
+ val contentUri: Uri ? = getContentUri(type)
83
96
84
97
val selection = " _id=?"
85
98
val selectionArgs = arrayOf(split[1 ])
@@ -106,6 +119,18 @@ object FileUtil {
106
119
return null
107
120
}
108
121
122
+ /* *
123
+ * checks the type of uri
124
+ */
125
+ private fun getContentUri (type : String ): Uri ? {
126
+ when (type) {
127
+ " image" -> return MediaStore .Images .Media .EXTERNAL_CONTENT_URI
128
+ " video" -> return MediaStore .Video .Media .EXTERNAL_CONTENT_URI
129
+ " audio" -> return MediaStore .Audio .Media .EXTERNAL_CONTENT_URI
130
+ }
131
+ return null
132
+ }
133
+
109
134
private fun getDataColumn (
110
135
context : Context ,
111
136
uri : Uri ? ,
@@ -118,7 +143,8 @@ object FileUtil {
118
143
val projection = arrayOf(column)
119
144
120
145
try {
121
- cursor = context.contentResolver.query(uri!! , projection, selection, selectionArgs, null )
146
+ cursor =
147
+ context.contentResolver.query(uri!! , projection, selection, selectionArgs, null )
122
148
if (cursor != null && cursor.moveToFirst()) {
123
149
val index = cursor.getColumnIndexOrThrow(column)
124
150
return cursor.getString(index)
0 commit comments