package com.example.base.extensions

import android.net.Uri
import com.example.base.utils.Utils
import java.io.File
import java.util.UUID

/**
 * 获取文件路径
 */
fun Uri.getFilePath(): String? {

    val path: String? = when (this.scheme) {
        "file" -> {
            this.path
        }
        "content" -> {
            val openIs = Utils.getApp().contentResolver.openInputStream(this)
            val file = File("${Utils.getApp().externalCacheDir}/${UUID.randomUUID()}.jpg")
            openIs?.toFile(file)
            file.absolutePath
        }
        else -> {   // "http", "https" , /storage/emulated/0/...
            this.toString()
        }
    }
    return path
}
