97 lines
3.6 KiB
Java
97 lines
3.6 KiB
Java
package com.ttstd.elderlyassistant.utils;
|
|
|
|
import android.content.ContentResolver;
|
|
import android.content.Context;
|
|
import android.database.Cursor;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
import android.os.Environment;
|
|
import android.provider.MediaStore;
|
|
import android.provider.OpenableColumns;
|
|
import android.util.Log;
|
|
|
|
import java.io.File;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
|
|
public class FileUtils {
|
|
private static final String TAG = "FileUtils";
|
|
|
|
public static String getCacheDir(Context context) {
|
|
String cachePath;
|
|
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|
|
|| !Environment.isExternalStorageRemovable()) {
|
|
if (context.getExternalCacheDir() != null) {
|
|
cachePath = context.getExternalCacheDir().getPath();
|
|
} else if (context.getExternalFilesDir("cache") != null) {
|
|
cachePath = context.getExternalFilesDir("cache").getPath();
|
|
} else {
|
|
cachePath = context.getCacheDir().getPath();
|
|
}
|
|
} else {
|
|
cachePath = context.getCacheDir().getPath();
|
|
}
|
|
Log.e(TAG, "getCacheDir: " + cachePath);
|
|
return cachePath;
|
|
}
|
|
|
|
public static File uriToFile(Context context, Uri uri) throws IOException {
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
// Android 10 以下用 MediaStore 查询
|
|
File file = uriToFileLegacy(context, uri);
|
|
if (file != null) return file;
|
|
}
|
|
// Android 10+ 或查询失败时使用沙盒复制
|
|
return uriToFileModern(context, uri);
|
|
}
|
|
|
|
public static File uriToFileLegacy(Context context, Uri uri) {
|
|
String[] projection = {MediaStore.Images.Media.DATA};
|
|
try (Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null)) {
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
|
String filePath = cursor.getString(columnIndex);
|
|
return new File(filePath);
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e("URI_TO_FILE", "Query failed: " + e.getMessage());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static File uriToFileModern(Context context, Uri uri) throws IOException {
|
|
ContentResolver resolver = context.getContentResolver();
|
|
String fileName = getFileName(resolver, uri); // 获取文件名(见下文辅助方法)
|
|
File cacheDir = context.getExternalCacheDir(); // 建议使用缓存目录
|
|
File outputFile = new File(cacheDir, fileName);
|
|
|
|
try (InputStream is = resolver.openInputStream(uri);
|
|
OutputStream os = new FileOutputStream(outputFile)) {
|
|
byte[] buffer = new byte[1024];
|
|
int len;
|
|
while ((len = is.read(buffer)) != -1) {
|
|
os.write(buffer, 0, len);
|
|
}
|
|
}
|
|
return outputFile;
|
|
}
|
|
|
|
// 获取文件名辅助方法
|
|
private static String getFileName(ContentResolver resolver, Uri uri) {
|
|
String name = null;
|
|
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
|
|
try (Cursor cursor = resolver.query(uri, null, null, null, null)) {
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
|
|
}
|
|
}
|
|
}
|
|
if (name == null) {
|
|
name = uri.getLastPathSegment(); // 备用方案
|
|
}
|
|
return name;
|
|
}
|
|
}
|