Skip to content

Commit

Permalink
[fix] fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mskteknasyon committed Dec 1, 2019
1 parent dc1147b commit 0c54c88
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 4 deletions.
195 changes: 195 additions & 0 deletions desk360/src/main/java/com/teknasyon/desk360/helper/ImageFilePath.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package com.teknasyon.desk360.helper

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore

//import android.provider.<span id="IL_AD11" class="IL_AD">MediaStore</span>;
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.KITKAT)
class ImageFilePath {
/* Get uri related content real local file path. */
fun getUriRealPath(ctx: Context, uri: Uri): String {
var ret = ""
ret = if (isAboveKitKat) { // Android OS above sdk version 19.
getUriRealPathAboveKitkat(ctx, uri)
} else { // Android OS below sdk version 19
getImageRealPath(ctx.contentResolver, uri, null)
}
return ret
}

fun getUriRealPathAboveKitkat(
ctx: Context?,
uri: Uri?
): String {
var ret = ""
if (ctx != null && uri != null) {
if (isContentUri(uri)) {

val documentId = DocumentsContract.getDocumentId(uri)
// Get uri authority.
val uriAuthority = uri.authority
if (isMediaDoc(uriAuthority)) {
val idArr = documentId.split(":").toTypedArray()
if (idArr.size == 2) { // First item is document type.
val docType = idArr[0]
// Second item is document real id.
val realDocId = idArr[1]
// Get content uri by document type.
var mediaContentUri =
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
if ("image" == docType) {
mediaContentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
} else if ("video" == docType) {
mediaContentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
} else if ("audio" == docType) {
mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
// Get where clause with real document id.
val whereClause =
MediaStore.Images.Media._ID + " = " + realDocId
ret =
getImageRealPath(ctx.contentResolver, mediaContentUri, whereClause)
}
} else if (isDownloadDoc(uriAuthority)) { // Build download uri.
val downloadUri =
Uri.parse("content://downloads/public_downloads")
// Append download document id at uri end.
val downloadUriAppendId =
ContentUris.withAppendedId(downloadUri, java.lang.Long.valueOf(documentId))
ret = getImageRealPath(ctx.contentResolver, downloadUriAppendId, null)
} else if (isExternalStoreDoc(uriAuthority)) {
val idArr = documentId.split(":").toTypedArray()
if (idArr.size == 2) {
val type = idArr[0]
val realDocId = idArr[1]
if ("primary".equals(type, ignoreCase = true)) {
ret =
Environment.getExternalStorageDirectory().toString() + "/" + realDocId
}
}
}
}
}
return ret
}

/* Check whether current android os version is bigger than kitkat or not. */
val isAboveKitKat: Boolean
get() {
var ret = false
ret = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
return ret
}

/* Check whether this uri represent a document or not. */
fun isDocumentUri(ctx: Context?, uri: Uri?): Boolean {
var ret = false
if (ctx != null && uri != null) {
ret = DocumentsContract.isDocumentUri(ctx, uri)
}
return ret
}

/* Check whether this uri is a content uri or not.
* content uri like content://media/external/images/media/1302716
* */
fun isContentUri(uri: Uri?): Boolean {
var ret = false
if (uri != null) {
val uriSchema = uri.scheme
if ("content".equals(uriSchema, ignoreCase = true)) {
ret = true
}
}
return ret
}

/* Check whether this uri is a file uri or not.
* file uri like file:///storage/41B7-12F1/DCIM/Camera/IMG_20180211_095139.jpg
* */
fun isFileUri(uri: Uri?): Boolean {
var ret = false
if (uri != null) {
val uriSchema = uri.scheme
if ("file".equals(uriSchema, ignoreCase = true)) {
ret = true
}
}
return ret
}

/* Check whether this document is provided by ExternalStorageProvider. */
fun isExternalStoreDoc(uriAuthority: String): Boolean {
var ret = false
if ("com.android.externalstorage.documents" == uriAuthority) {
ret = true
}
return ret
}

/* Check whether this document is provided by DownloadsProvider. */
fun isDownloadDoc(uriAuthority: String): Boolean {
var ret = false
if ("com.android.providers.downloads.documents" == uriAuthority) {
ret = true
}
return ret
}

/* Check whether this document is provided by MediaProvider. */
fun isMediaDoc(uriAuthority: String): Boolean {
var ret = false
if ("com.android.providers.media.documents" == uriAuthority) {
ret = true
}
return ret
}

/* Check whether this document is provided by google photos. */
fun isGooglePhotoDoc(uriAuthority: String): Boolean {
var ret = false
if ("com.google.android.apps.photos.content" == uriAuthority) {
ret = true
}
return ret
}

/* Return uri represented document file real local path.*/
fun getImageRealPath(
contentResolver: ContentResolver,
uri: Uri,
whereClause: String?
): String {
var ret = ""
// Query the uri with condition.
val cursor =
contentResolver.query(uri, null, whereClause, null, null)
if (cursor != null) {
val moveToFirst = cursor.moveToFirst()
if (moveToFirst) { // Get columns name by uri type.
var columnName = MediaStore.Images.Media.DATA
if (uri === MediaStore.Images.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Images.Media.DATA
} else if (uri === MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Audio.Media.DATA
} else if (uri === MediaStore.Video.Media.EXTERNAL_CONTENT_URI) {
columnName = MediaStore.Video.Media.DATA
}
// Get column index.
val imageColumnIndex = cursor.getColumnIndex(columnName)
// Get column value which is the uri related file local path.
ret = cursor.getString(imageColumnIndex)
}
}
return ret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
Expand Down
4 changes: 2 additions & 2 deletions desk360/src/main/res/layout/desk360_add_new_ticket_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
android:drawableStart="@drawable/path_icon_desk360"
android:drawableLeft="@drawable/path_icon_desk360"
android:drawablePadding="8dp"
android:text="dsdsdsds"
android:text="Dosya Seçiniz"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/create_screen_root_view"
tools:ignore="MissingConstraints" />
Expand All @@ -80,7 +80,7 @@
android:padding="16dp"
android:text="sabir..jpg"
android:textSize="14sp"
android:visibility="visible"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/create_screen_root_view" />

Expand Down
13 changes: 13 additions & 0 deletions desk360/src/main/res/xml/my_path.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="." />
<external-files-path
name="external_files"
path="." />
<!-- FOR SD CARD-->
<root-path
name="sdcard1"
path="." />
</paths>

0 comments on commit 0c54c88

Please sign in to comment.