Runt
2025-10-11 89e35a1933ba40513a96572b27291c0aa65c918c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.runt.open.mvi.utils
 
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import com.runt.open.mvi.OpenApplication
import java.io.File
import java.io.FileInputStream
 
/**
 * @author Runt(qingingrunt2010@qq.com)
 * @purpose
 * @date 10/7/25
 */
class FileUtils {
 
    companion object{
        val TAG = "FileUtils";
 
        fun getFilePathFromUri(uri: Uri):String?{
            var filePath = "";
            var filePathColumn = arrayOf(MediaStore.Video.Media.DATA)
            var cursor = OpenApplication.getApplication()!!.contentResolver.query(uri!!,filePathColumn,null,null,null)
            cursor?.let {
                it.moveToFirst()
                var index = it.getColumnIndex(filePathColumn[0])
                if(index > -1 && index < it.columnCount){
                    filePath = it.getString(index)
                }
                it.close();
            }
            var file = File(filePath);
            Log.i(TAG,"文件是否存在=${file.exists()} file=${filePath} ")
            if(file.exists()){
                try {
                    var inputStream = FileInputStream(file)
                    inputStream.close();
                    return filePath;
                }catch (e:Exception){
                    return null;
                }
            }else{
                return null;
            }
        }
    }
 
}