Skip to content

Extra checks added whie getting absolute file path in FileUtil #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 51 additions & 25 deletions app/src/main/java/com/github/code/gambit/utility/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.code.gambit.utility
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.database.CursorIndexOutOfBoundsException
import android.net.Uri
import android.provider.DocumentsContract
import android.provider.MediaStore
Expand All @@ -15,37 +16,61 @@ object FileUtil {
fun Uri.isSchemeTypeContent(): Boolean = "content".equals(this.scheme!!, ignoreCase = true)

fun getPathFromLocalUri(context: Context, uri: Uri): String? {
val path: String? = try {
_getPathFromLocalUri(context, uri)
} catch (exp: CursorIndexOutOfBoundsException) {
exp.printStackTrace()
uri.path
} catch (exp: NullPointerException) {
exp.printStackTrace()
uri.path
} catch (exp: NumberFormatException) {
exp.printStackTrace()
uri.path
}
return path?.let {
if (File(it).exists()) {
path
} else {
null
}
}
}

fun _getPathFromLocalUri(context: Context, uri: Uri): String? {
// DocumentProvider
when {
DocumentsContract.isDocumentUri(context, uri) -> {
// ExternalStorageProvider
when {
isExternalStorageDocument(uri) -> {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val split =
docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]

// This is for checking Main Memory
return if ("primary".equals(type, ignoreCase = true)) {
if ("primary".equals(type, ignoreCase = true)) {
if (split.size > 1) {
context.getExternalFilesDir(null).toString() + "/" + split[1]
return context.getExternalFilesDir(null).toString() + "/" + split[1]
} else {
context.getExternalFilesDir(null).toString() + "/"
return context.getExternalFilesDir(null).toString() + "/"
}
// This is for checking SD Card
} else {
} /*else {
val path = "storage" + "/" + docId.replace(":", "/")
if (File(path).exists()) {
path
} else {
"/storage/sdcard/" + split[1]
}
}
}*/
}
isDownloadsDocument(uri) -> {
val fileName = getFilePath(context, uri)
if (fileName != null) {
val path = context.getExternalFilesDir(null).toString() + "/Download/" + fileName
val path = context.getExternalFilesDir(null)
.toString() + "/Download/" + fileName
if (File(path).exists()) {
return path
}
Expand All @@ -56,30 +81,18 @@ object FileUtil {
id = id.split(":")[1]
}
val contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id)
Uri.parse("content://downloads/public_downloads"),
java.lang.Long.valueOf(id)
)
return getDataColumn(context, contentUri, null, null)
}
isMediaDocument(uri) -> {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val split =
docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]

var contentUri: Uri? = null
when (type) {
"image" -> {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}
"video" -> {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
"audio" -> {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
"document" -> {
contentUri = Uri.parse("content://media/external/documents/media")
}
}
val contentUri: Uri? = getContentUri(type)

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

/**
* checks the type of uri
*/
private fun getContentUri(type: String): Uri? {
when (type) {
"image" -> return MediaStore.Images.Media.EXTERNAL_CONTENT_URI
"video" -> return MediaStore.Video.Media.EXTERNAL_CONTENT_URI
"audio" -> return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
return null
}

private fun getDataColumn(
context: Context,
uri: Uri?,
Expand All @@ -118,7 +143,8 @@ object FileUtil {
val projection = arrayOf(column)

try {
cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
cursor =
context.contentResolver.query(uri!!, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val index = cursor.getColumnIndexOrThrow(column)
return cursor.getString(index)
Expand Down