package cn.sinata.xldutils.utils
|
|
import android.app.Activity
|
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.os.StatFs
|
import android.provider.DocumentsContract
|
import android.provider.MediaStore
|
import android.util.Log
|
import java.lang.NumberFormatException
|
|
/**
|
*
|
*/
|
|
fun Any.getSDFreeSize():Long{
|
//取得SD卡文件路径
|
val path = Environment.getExternalStorageDirectory()
|
val sf = StatFs(path.path)
|
//获取单个数据块的大小(Byte)
|
val blockSize: Long
|
//空闲的数据块的数量
|
val freeBlocks: Long
|
if (Build.VERSION.SDK_INT >= 18) {
|
blockSize = sf.blockSizeLong
|
freeBlocks = sf.availableBlocksLong
|
} else {
|
blockSize = sf.blockSize.toLong()
|
freeBlocks = sf.availableBlocks.toLong()
|
}
|
//返回SD卡空闲大小
|
//return freeBlocks * blockSize; //单位Byte
|
//return (freeBlocks * blockSize)/1024; //单位KB
|
return freeBlocks * blockSize / 1024 / 1024 //单位MB
|
}
|
|
fun Activity.getUrlPath(imageUri: Uri?) :String?{
|
if (imageUri == null) {
|
return null
|
}
|
Log.e("mmp","文档uri:"+imageUri)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(this, imageUri)) {
|
if (isExternalStorageDocument(imageUri)) {
|
val docId = DocumentsContract.getDocumentId(imageUri)
|
Log.e("mmp","外存文件,文档id:"+docId)
|
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
val type = split[0]
|
if ("primary".equals(type, ignoreCase = true)) {
|
return Environment.getExternalStorageDirectory().toString() + "/" + split[1]
|
}
|
} else if (isDownloadsDocument(imageUri)) {
|
val id = DocumentsContract.getDocumentId(imageUri)
|
Log.e("mmp","下载文件,文档id:"+id)
|
return try {
|
val contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id))
|
getDataColumn(this, contentUri, null, null)
|
}catch (e:NumberFormatException){
|
null
|
}
|
} else if (isMediaDocument(imageUri)) {
|
val docId = DocumentsContract.getDocumentId(imageUri)
|
Log.e("mmp","多媒体文件,文档id:"+docId)
|
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 = MediaStore.Images.Media._ID + "=?"
|
val selectionArgs = arrayOf(split[1])
|
return getDataColumn(this, contentUri, selection, selectionArgs)
|
}
|
} // MediaStore (and general)
|
else if ("content".equals(imageUri.scheme, ignoreCase = true)) {
|
Log.e("mmp","content文件")
|
// Return the remote address
|
if (isGooglePhotosUri(imageUri))
|
return imageUri.lastPathSegment
|
return getDataColumn(this, imageUri, null, null)
|
} else if ("file".equals(imageUri.scheme, ignoreCase = true)) {
|
Log.e("mmp","file")
|
return imageUri.path
|
}// File
|
return null
|
}
|
|
/**
|
* @param uri The Uri to check.
|
* @return Whether the Uri authority is Google Photos.
|
*/
|
fun isGooglePhotosUri(uri: Uri): Boolean = "com.google.android.apps.photos.content" == uri.authority
|
|
/**
|
* @param uri The Uri to check.
|
* *
|
* @return Whether the Uri authority is ExternalStorageProvider.
|
*/
|
fun isExternalStorageDocument(uri: Uri): Boolean =
|
"com.android.externalstorage.documents" == uri.authority
|
|
/**
|
* @param uri The Uri to check.
|
* *
|
* @return Whether the Uri authority is DownloadsProvider.
|
*/
|
fun isDownloadsDocument(uri: Uri): Boolean =
|
"com.android.providers.downloads.documents" == uri.authority
|
|
/**
|
* @param uri The Uri to check.
|
* *
|
* @return Whether the Uri authority is MediaProvider.
|
*/
|
fun isMediaDocument(uri: Uri): Boolean = "com.android.providers.media.documents" == uri.authority
|
|
fun getDataColumn(context: Context, uri: Uri?, selection: String?, selectionArgs: Array<String>?): String? {
|
if (uri == null) {
|
return null
|
}
|
var cursor: Cursor? = null
|
val column = MediaStore.Images.Media.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 {
|
if (cursor != null)
|
cursor.close()
|
}
|
return null
|
}
|
|
fun Activity.getVersionName():String {
|
return try {
|
val manager = this.packageManager
|
val info = manager.getPackageInfo(this.packageName, 0)
|
info.versionName
|
} catch (e: Exception) {
|
e.printStackTrace()
|
"--"
|
}
|
|
}
|