fix: 修复谷歌上报的崩溃
This commit is contained in:
@@ -93,7 +93,7 @@ abstract class PickImageActionNew protected constructor(
|
||||
}
|
||||
|
||||
fun copyFile(context: Context, uri: Uri): File? {
|
||||
val fileName = getName(context, uri) ?: "defName.jpg"
|
||||
var fileName = getName(context, uri)
|
||||
|
||||
val outPutDir = context.getExternalFilesDir(null) ?: return null
|
||||
|
||||
@@ -112,13 +112,14 @@ abstract class PickImageActionNew protected constructor(
|
||||
|
||||
}
|
||||
|
||||
fun getName(context: Context, uri: Uri): String? {
|
||||
var fileName: String? = null
|
||||
val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME)
|
||||
val cursor: Cursor? = context.contentResolver.query(
|
||||
uri, projection, null, null, null
|
||||
)
|
||||
fun getName(context: Context, uri: Uri,isImg:Boolean = true): String {
|
||||
var fileName = ""
|
||||
try {
|
||||
val projection = arrayOf(MediaStore.MediaColumns.DISPLAY_NAME)
|
||||
val cursor: Cursor? = context.contentResolver.query(
|
||||
uri, projection, null, null, null
|
||||
)
|
||||
|
||||
cursor?.use {
|
||||
if (it.moveToFirst()) {
|
||||
fileName =
|
||||
@@ -126,7 +127,44 @@ abstract class PickImageActionNew protected constructor(
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
fileName=""
|
||||
}
|
||||
|
||||
if (fileName == "") {
|
||||
val type = getFileType(context, uri)
|
||||
var fileEnd: String
|
||||
|
||||
if (isImg) {
|
||||
if (type == "image/gif") {
|
||||
fileEnd = ".gif"
|
||||
}else if (type == "image/png") {
|
||||
fileEnd = ".png"
|
||||
}else if (type == "image/jpeg") {
|
||||
fileEnd = ".jpeg"
|
||||
}else if (type == "image/bmp") {
|
||||
fileEnd = ".bmp"
|
||||
}else if (type == "image/webp") {
|
||||
fileEnd = ".webp"
|
||||
} else {
|
||||
fileEnd = ".jpg"
|
||||
}
|
||||
} else {
|
||||
fileEnd=".mp3"
|
||||
}
|
||||
fileName = "img_${System.currentTimeMillis()}$fileEnd"
|
||||
}
|
||||
return fileName
|
||||
}
|
||||
|
||||
fun getFileType(context: Context, uri: Uri): String {
|
||||
var type = ""
|
||||
try {
|
||||
context.contentResolver?.let {
|
||||
type = it.getType(uri).toString()
|
||||
}
|
||||
}catch (e:Exception){
|
||||
|
||||
}
|
||||
return type
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user