Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
update get path function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mystery00 committed Jul 14, 2018
1 parent 8d7eaac commit 2197e74
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

abstract class BaseRecyclerViewAdapter<T : RecyclerView.ViewHolder, in C : Any>(private val context: Context?,
@LayoutRes private val itemLayoutId: Int,
private val list: ArrayList<in C>) : RecyclerView.Adapter<T>() {
abstract class BaseRecyclerViewAdapter<T : RecyclerView.ViewHolder, in C : Any>
(private val context: Context?,
@LayoutRes private val itemLayoutId: Int,
private val list: ArrayList<in C>) : RecyclerView.Adapter<T>() {
override fun getItemCount(): Int = list.size

override fun onBindViewHolder(holder: T, position: Int) {
Expand Down
77 changes: 41 additions & 36 deletions tools/src/main/java/vip/mystery0/tools/utils/FileTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package vip.mystery0.tools.utils

import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
Expand All @@ -42,48 +43,52 @@ object FileTools {
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
fun getPath(context: Context, uri: Uri): String? {
when {
DocumentsContract.isDocumentUri(context, uri) -> {
when (uri.authority) {
"com.android.externalstorage.documents" -> {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]

if ("primary".equals(type, ignoreCase = true)) {
return Environment.getExternalStorageDirectory().toString() + File.pathSeparator + split[1]
when (uri.scheme) {
ContentResolver.SCHEME_FILE -> return uri.path
ContentResolver.SCHEME_CONTENT -> {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
var path: String? = null
val cursor = context.contentResolver.query(uri, arrayOf(MediaStore.Images.Media.DATA), null, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
val columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
if (columnIndex > -1)
path = cursor.getString(columnIndex)
}
cursor.close()
}
"com.android.providers.downloads.documents" -> {
val id = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), id.toLong())

return getDataColumn(context, contentUri, null, null)
}
"com.android.providers.media.documents" -> {
val docId = DocumentsContract.getDocumentId(uri)
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
return path
} else if (DocumentsContract.isDocumentUri(context, uri)) {
when (uri.authority) {
"com.android.externalstorage.documents" -> {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val type = split[0]
if (type.toLowerCase() == "primary")
return Environment.getExternalStorageDirectory().toString() + File.pathSeparator + split[1]
}
"com.android.providers.downloads.documents" -> {
val id = DocumentsContract.getDocumentId(uri)
val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), id.toLong())
return getDataColumn(context, contentUri, null, null)
}
"com.android.providers.media.documents" -> {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(':')
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
}
val selection = "_id=?"
val selectionArgs = arrayOf(split[1])
return getDataColumn(context, contentUri!!, selection, selectionArgs)
}

val selection = "_id=?"
val selectionArgs = arrayOf(split[1])

return getDataColumn(context, contentUri!!, selection, selectionArgs)
}
}
}
"content".equals(uri.scheme, ignoreCase = true) ->
return getDataColumn(context, uri, null, null)
"file".equals(uri.scheme, ignoreCase = true) ->
return uri.path
}
return null
}
Expand Down

0 comments on commit 2197e74

Please sign in to comment.