Skip to content

Commit 6850d9f

Browse files
Merge pull request #94 from danishjamal104/fix-93
Extra checks added whie getting absolute file path in FileUtil
2 parents f7f444b + 34e14b4 commit 6850d9f

File tree

1 file changed

+51
-25
lines changed
  • app/src/main/java/com/github/code/gambit/utility

1 file changed

+51
-25
lines changed

app/src/main/java/com/github/code/gambit/utility/FileUtil.kt

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.code.gambit.utility
33
import android.content.ContentUris
44
import android.content.Context
55
import android.database.Cursor
6+
import android.database.CursorIndexOutOfBoundsException
67
import android.net.Uri
78
import android.provider.DocumentsContract
89
import android.provider.MediaStore
@@ -15,37 +16,61 @@ object FileUtil {
1516
fun Uri.isSchemeTypeContent(): Boolean = "content".equals(this.scheme!!, ignoreCase = true)
1617

1718
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? {
1841
// DocumentProvider
1942
when {
2043
DocumentsContract.isDocumentUri(context, uri) -> {
2144
// ExternalStorageProvider
2245
when {
2346
isExternalStorageDocument(uri) -> {
2447
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()
2650
val type = split[0]
2751

2852
// This is for checking Main Memory
29-
return if ("primary".equals(type, ignoreCase = true)) {
53+
if ("primary".equals(type, ignoreCase = true)) {
3054
if (split.size > 1) {
31-
context.getExternalFilesDir(null).toString() + "/" + split[1]
55+
return context.getExternalFilesDir(null).toString() + "/" + split[1]
3256
} else {
33-
context.getExternalFilesDir(null).toString() + "/"
57+
return context.getExternalFilesDir(null).toString() + "/"
3458
}
3559
// This is for checking SD Card
36-
} else {
60+
} /*else {
3761
val path = "storage" + "/" + docId.replace(":", "/")
3862
if (File(path).exists()) {
3963
path
4064
} else {
4165
"/storage/sdcard/" + split[1]
4266
}
43-
}
67+
}*/
4468
}
4569
isDownloadsDocument(uri) -> {
4670
val fileName = getFilePath(context, uri)
4771
if (fileName != null) {
48-
val path = context.getExternalFilesDir(null).toString() + "/Download/" + fileName
72+
val path = context.getExternalFilesDir(null)
73+
.toString() + "/Download/" + fileName
4974
if (File(path).exists()) {
5075
return path
5176
}
@@ -56,30 +81,18 @@ object FileUtil {
5681
id = id.split(":")[1]
5782
}
5883
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)
6086
)
6187
return getDataColumn(context, contentUri, null, null)
6288
}
6389
isMediaDocument(uri) -> {
6490
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()
6693
val type = split[0]
6794

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)
8396

8497
val selection = "_id=?"
8598
val selectionArgs = arrayOf(split[1])
@@ -106,6 +119,18 @@ object FileUtil {
106119
return null
107120
}
108121

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+
109134
private fun getDataColumn(
110135
context: Context,
111136
uri: Uri?,
@@ -118,7 +143,8 @@ object FileUtil {
118143
val projection = arrayOf(column)
119144

120145
try {
121-
cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
146+
cursor =
147+
context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
122148
if (cursor != null && cursor.moveToFirst()) {
123149
val index = cursor.getColumnIndexOrThrow(column)
124150
return cursor.getString(index)

0 commit comments

Comments
 (0)