-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Sample app made compatible with Android Kitkat 4.4+ (API 19) - Fixed Uri to File Conversion issue #8 - (Special Thanks to squeeish) - Added Support for Inline Activity Result(Special Thanks to soareseneves) Fixed App crash issue reported here #6.
- Loading branch information
1 parent
5800fef
commit c398730
Showing
10 changed files
with
227 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
imagepicker-support/src/main/kotlin/com/github/dhaval2404/imagepicker/util/FileUriUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package com.github.dhaval2404.imagepicker.util | ||
|
||
import android.content.ContentUris | ||
import android.content.Context | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.provider.DocumentsContract | ||
import android.provider.MediaStore | ||
import java.io.File | ||
|
||
/** | ||
* https://gist.github.com/HBiSoft/15899990b8cd0723c3a894c1636550a8 | ||
* | ||
* This class acts as a drop in replacement for uCrop FileUtils class | ||
*/ | ||
|
||
object FileUriUtils { | ||
|
||
fun getRealPath(context: Context, uri: Uri): String? { | ||
|
||
val isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT | ||
|
||
// DocumentProvider | ||
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { | ||
// ExternalStorageProvider | ||
if (isExternalStorageDocument(uri)) { | ||
val docId = DocumentsContract.getDocumentId(uri) | ||
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 (split.size > 1) { | ||
Environment.getExternalStorageDirectory().toString() + "/" + split[1] | ||
} else { | ||
Environment.getExternalStorageDirectory().toString() + "/" | ||
} | ||
// This is for checking SD Card | ||
} else { | ||
val path = "storage" + "/" + docId.replace(":", "/") | ||
if(File(path).exists()){ | ||
path | ||
}else{ | ||
"/storage/sdcard/"+split[1]; | ||
} | ||
} | ||
|
||
} else if (isDownloadsDocument(uri)) { | ||
val fileName = getFilePath(context, uri) | ||
if (fileName != null) { | ||
return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName | ||
} | ||
|
||
val id = DocumentsContract.getDocumentId(uri) | ||
val contentUri = ContentUris.withAppendedId( | ||
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id) | ||
) | ||
return getDataColumn(context, contentUri, null, null) | ||
} else if (isMediaDocument(uri)) { | ||
val docId = DocumentsContract.getDocumentId(uri) | ||
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
val type = split[0] | ||
|
||
var contentUri: Uri? = null | ||
if ("image" == type) { | ||
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI | ||
} else if ("video" == type) { | ||
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI | ||
} else if ("audio" == type) { | ||
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI | ||
} | ||
|
||
val selection = "_id=?" | ||
val selectionArgs = arrayOf(split[1]) | ||
|
||
return getDataColumn(context, contentUri, selection, selectionArgs) | ||
}// MediaProvider | ||
// DownloadsProvider | ||
} else if ("content".equals(uri.scheme!!, ignoreCase = true)) { | ||
|
||
// Return the remote address | ||
return if (isGooglePhotosUri(uri)) uri.lastPathSegment else getDataColumn(context, uri, null, null) | ||
|
||
} else if ("file".equals(uri.scheme!!, ignoreCase = true)) { | ||
return uri.path | ||
}// File | ||
// MediaStore (and general) | ||
|
||
return null | ||
} | ||
|
||
private fun getDataColumn( | ||
context: Context, uri: Uri?, selection: String?, | ||
selectionArgs: Array<String>? | ||
): String? { | ||
|
||
var cursor: Cursor? = null | ||
val column = "_data" | ||
val projection = arrayOf(column) | ||
|
||
try { | ||
cursor = context.contentResolver.query(uri!!, projection, selection, selectionArgs, null) | ||
if (cursor != null && cursor.moveToFirst()) { | ||
val index = cursor.getColumnIndexOrThrow(column) | ||
return cursor.getString(index) | ||
} | ||
} finally { | ||
cursor?.close() | ||
} | ||
return null | ||
} | ||
|
||
|
||
private fun getFilePath(context: Context, uri: Uri): String? { | ||
|
||
var cursor: Cursor? = null | ||
val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME) | ||
|
||
try { | ||
cursor = context.contentResolver.query(uri, projection, null, null, null) | ||
if (cursor != null && cursor.moveToFirst()) { | ||
val index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME) | ||
return cursor.getString(index) | ||
} | ||
} finally { | ||
cursor?.close() | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is ExternalStorageProvider. | ||
*/ | ||
private fun isExternalStorageDocument(uri: Uri): Boolean { | ||
return "com.android.externalstorage.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is DownloadsProvider. | ||
*/ | ||
private fun isDownloadsDocument(uri: Uri): Boolean { | ||
return "com.android.providers.downloads.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is MediaProvider. | ||
*/ | ||
private fun isMediaDocument(uri: Uri): Boolean { | ||
return "com.android.providers.media.documents" == uri.authority | ||
} | ||
|
||
/** | ||
* @param uri The Uri to check. | ||
* @return Whether the Uri authority is Google Photos. | ||
*/ | ||
private fun isGooglePhotosUri(uri: Uri): Boolean { | ||
return "com.google.android.apps.photos.content" == uri.authority | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.