登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
Gitee AI
NEW
我知道了
查看详情
登录
注册
代码拉取完成,页面将自动刷新
开源项目
>
手机/移动开发
>
Android组件/项目
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
5
Star
43
Fork
19
黎明白昼
/
HQW
代码
Issues
16
Pull Requests
0
Wiki
统计
流水线
服务
Gitee Pages
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
我知道了,不再自动展开
更新失败,请稍后重试!
Issues
/
详情
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
文件选择功能需要改变
待办的
#I92D6H
黎明白昼
拥有者
创建于
2024-02-20 14:26
``` package com.tencent.qcloud.tuikit.timcommon.util; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.provider.OpenableColumns; import android.text.TextUtils; import android.util.Log; import android.webkit.MimeTypeMap; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.tencent.qcloud.tuicore.ServiceInitializer; import com.tencent.qcloud.tuicore.TUIConfig; import com.tencent.qcloud.tuicore.TUILogin; import com.tencent.qcloud.tuikit.timcommon.R; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; import java.util.Random; public class FileUtil { public static final String DOCUMENTS_DIR = "documents"; public static final String FILE_PROVIDER_AUTH = ".timcommon.fileprovider"; public static final int SIZETYPE_B = 1; public static final int SIZETYPE_KB = 2; public static final int SIZETYPE_MB = 3; public static final int SIZETYPE_GB = 4; public static boolean deleteFile(String path) { if (TextUtils.isEmpty(path)) { return false; } boolean result = false; File file = new File(path); if (file.exists()) { result = file.delete(); } return result; } public static String getPathFromUri(Uri uri) { String path = ""; try { int sdkVersion = Build.VERSION.SDK_INT; if (sdkVersion >= 19) { path = getPathByCopyFile(TUILogin.getAppContext(), uri); } else { path = getRealFilePath(uri); } } catch (Exception e) { e.printStackTrace(); } if (path == null) { path = ""; } return path; } public static String getRealFilePath(Uri uri) { if (null == uri) { return null; } final String scheme = uri.getScheme(); String data = null; if (scheme == null) { data = uri.getPath(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = TUILogin.getAppContext().getContentResolver().query(uri, new String[] {MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; } public static Uri getUriFromPath(String path) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return FileProvider.getUriForFile( TUIConfig.getAppContext(), TUIConfig.getAppContext().getApplicationInfo().packageName + FILE_PROVIDER_AUTH, new File(path)); } else { return Uri.fromFile(new File(path)); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 专为Android4.4以上设计的从Uri获取文件路径 * * Get file path from Uri specially designed for Android4.4 and above */ public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } else { return getPathByCopyFile(context, uri); } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); if (id.startsWith("raw:")) { final String path = id.replaceFirst("raw:", ""); return path; } String[] contentUriPrefixesToTry = new String[] {"content://downloads/public_downloads", "content://downloads/my_downloads", "content://downloads/all_downloads"}; for (String contentUriPrefix : contentUriPrefixesToTry) { Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.parseLong(id)); try { String path = getDataColumn(context, contentUri, null, null); if (path != null && Build.VERSION.SDK_INT < 29) { return path; } } catch (Exception e) { e.printStackTrace(); } } // 在某些android8+的手机上,无法获取路径,所以用拷贝的方式,获取新文件名,然后把文件发出去 // On some android8+ mobile phones, the path cannot be obtained, so the new file name is obtained by copying, and then the file is sent out return getPathByCopyFile(context, uri); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] {split[1]}; String path = getDataColumn(context, contentUri, selection, selectionArgs); if (TextUtils.isEmpty(path) || Build.VERSION.SDK_INT >= 29) { path = getPathByCopyFile(context, uri); } return path; } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { String path = getDataColumn(context, uri, null, null); if (TextUtils.isEmpty(path) || Build.VERSION.SDK_INT >= 29) { // 在某些华为android9+的手机上,无法获取路径,所以用拷贝的方式,获取新文件名,然后把文件发出去 path = getPathByCopyFile(context, uri); } return path; } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } private static String getPathByCopyFile(Context context, Uri uri) { String fileName = getFileName(context, uri); File cacheDir = getDocumentCacheDir(context); File file = generateFileName(fileName, cacheDir); String destinationPath = null; if (file != null) { destinationPath = file.getAbsolutePath(); boolean saveSuccess = saveFileFromUri(context, uri, destinationPath); if (!saveSuccess) { file.delete(); return null; } } return destinationPath; } @Nullable public static File generateFileName(@Nullable String name, File directory) { if (name == null) { return null; } File file = new File(directory, name); if (file.exists()) { String fileName = name; String extension = ""; int dotIndex = name.lastIndexOf('.'); if (dotIndex > 0) { fileName = name.substring(0, dotIndex); extension = name.substring(dotIndex); } int index = 0; while (file.exists()) { index++; name = fileName + '(' + index + ')' + extension; file = new File(directory, name); } } try { if (!file.createNewFile()) { return null; } } catch (IOException e) { e.printStackTrace(); return null; } return file; } public static String getFileName(@NonNull Context context, Uri uri) { String mimeType = context.getContentResolver().getType(uri); String filename = null; if (mimeType == null && context != null) { filename = getName(uri.toString()); } else { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); if (returnCursor != null) { int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); returnCursor.moveToFirst(); filename = returnCursor.getString(nameIndex); returnCursor.close(); } } return filename; } public static String getName(String filePath) { if (filePath == null) { return null; } int index = filePath.lastIndexOf('/'); return filePath.substring(index + 1); } public static File getDocumentCacheDir(@NonNull Context context) { File dir = new File(context.getCacheDir(), DOCUMENTS_DIR); if (!dir.exists()) { dir.mkdirs(); } return dir; } private static boolean saveFileFromUri(Context context, Uri uri, String destinationPath) { InputStream is = null; BufferedOutputStream bos = null; try { is = context.getContentResolver().openInputStream(uri); bos = new BufferedOutputStream(new FileOutputStream(destinationPath, false)); byte[] buf = new byte[1024]; int actualBytes; while ((actualBytes = is.read(buf)) != -1) { bos.write(buf, 0, actualBytes); } } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (is != null) { is.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * 转换文件大小 * * Convert file size to string * * @param fileS * @return */ public static String formatFileSize(long fileS) { DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("#.00", symbols); String fileSizeString = ""; String wrongSize = "0B"; if (fileS == 0) { return wrongSize; } if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "KB"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "GB"; } return fileSizeString; } // 修复 android.webkit.MimeTypeMap 的 getFileExtensionFromUrl 方法不支持中文的问题 // fix the problem that getFileExtensionFromUrl does not support Chinese public static String getFileExtensionFromUrl(String url) { if (!TextUtils.isEmpty(url)) { int fragment = url.lastIndexOf('#'); if (fragment > 0) { url = url.substring(0, fragment); } int query = url.lastIndexOf('?'); if (query > 0) { url = url.substring(0, query); } int filenamePos = url.lastIndexOf('/'); String filename = 0 <= filenamePos ? url.substring(filenamePos + 1) : url; // if the filename contains special characters, we don't // consider it valid for our matching purposes: // 去掉正则表达式判断以添加中文支持 // if (!filename.isEmpty() && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) if (!filename.isEmpty()) { int dotPos = filename.lastIndexOf('.'); if (0 <= dotPos) { return filename.substring(dotPos + 1).toLowerCase(); } } } return ""; } public static void openFile(String path, String fileName) { Uri uri = FileProvider.getUriForFile( TUIConfig.getAppContext(), TUIConfig.getAppContext().getApplicationInfo().packageName + FILE_PROVIDER_AUTH, new File(path)); if (uri == null) { Log.e("FileUtil", "openFile failed , uri is null"); return; } String fileExtension; if (TextUtils.isEmpty(fileName)) { fileExtension = FileUtil.getFileExtensionFromUrl(path); } else { fileExtension = FileUtil.getFileExtensionFromUrl(fileName); } String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(uri, mimeType); try { Intent chooserIntent = Intent.createChooser(intent, TUIConfig.getAppContext().getString(R.string.open_file_tips)); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); TUIConfig.getAppContext().startActivity(chooserIntent); } catch (Exception e) { Log.e("FileUtil", "openFile failed , " + e.getMessage()); } } public static long getFileSize(String path) { File file = new File(path); if (file.exists()) { return file.length(); } return 0; } public static String generateImageFilePath() { String name = System.nanoTime() + "_" + Math.abs(new Random().nextInt()); return TUIConfig.getImageBaseDir() + name + ".jpg"; } public static String generateExternalStorageImageFilePath() { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + TUIConfig.getAppContext().getPackageName() + TUIConfig.IMAGE_BASE_DIR_SUFFIX); if (!dir.exists()) { dir.mkdirs(); } return dir.getAbsolutePath() + File.separatorChar + System.nanoTime() + "_" + Math.abs(new Random().nextInt()) + ".jpg"; } public static String generateVideoFilePath() { String name = System.nanoTime() + "_" + Math.abs(new Random().nextInt()); return TUIConfig.getVideoBaseDir() + name + ".mp4"; } public static String generateExternalStorageVideoFilePath() { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + TUIConfig.getAppContext().getPackageName() + TUIConfig.VIDEO_BASE_DIR_SUFFIX); if (!dir.exists()) { dir.mkdirs(); } return dir.getAbsolutePath() + File.separatorChar + System.nanoTime() + "_" + Math.abs(new Random().nextInt()) + ".mp4"; } public static boolean saveBitmap(String path, Bitmap b) { try { FileOutputStream fout = new FileOutputStream(path); BufferedOutputStream bos = new BufferedOutputStream(fout); b.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public static boolean isFileExists(String path) { try { File file = new File(path); return file.exists() && file.isFile(); } catch (Exception e) { return false; } } public static boolean isDirExists(String path) { try { File file = new File(path); return file.exists() && file.isDirectory(); } catch (Exception e) { return false; } } public static boolean isFileSizeExceedsLimit(Uri data, int maxSize) { try { Cursor returnCursor = ServiceInitializer.getAppContext().getContentResolver().query(data, null, null, null, null); if (returnCursor != null) { int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE); returnCursor.moveToFirst(); int size = returnCursor.getInt(sizeIndex); if (size > maxSize) { return true; } returnCursor.close(); return false; } return false; } catch (Exception e) { return false; } } } ``` ``` /*******************************HQW系统文件选择********************************/ /** * HQW系统文件选择 */ public void HQWFileSelect(HQWFileListener hqwFileListener) { this.hqwFileListener = hqwFileListener; fileLauncher.launch(""); } /** * 获取文件 */ ActivityResultLauncher fileLauncher = registerForActivityResult(new ActivityResultContract<String, Uri>() { @Override public Uri parseResult(int i, @Nullable Intent intent) { if (intent!=null){ if (intent.getData()!=null){ return intent.getData(); }else { return null; } }else { return null; } } @NonNull @Override public Intent createIntent(@NonNull Context context, String s) { return new Intent(Intent.ACTION_OPEN_DOCUMENT) .setType("*/*"); } }, new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri uri) { if (uri!=null){ hqwFileListener.file(uri); String path= HQWFileUtil.UritoFilePath(HQWAppCompatActivity.this,uri); if (!TextUtils.isEmpty(path)){ hqwFileListener.file(path); } } } }); ```
``` package com.tencent.qcloud.tuikit.timcommon.util; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.provider.OpenableColumns; import android.text.TextUtils; import android.util.Log; import android.webkit.MimeTypeMap; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.tencent.qcloud.tuicore.ServiceInitializer; import com.tencent.qcloud.tuicore.TUIConfig; import com.tencent.qcloud.tuicore.TUILogin; import com.tencent.qcloud.tuikit.timcommon.R; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; import java.util.Random; public class FileUtil { public static final String DOCUMENTS_DIR = "documents"; public static final String FILE_PROVIDER_AUTH = ".timcommon.fileprovider"; public static final int SIZETYPE_B = 1; public static final int SIZETYPE_KB = 2; public static final int SIZETYPE_MB = 3; public static final int SIZETYPE_GB = 4; public static boolean deleteFile(String path) { if (TextUtils.isEmpty(path)) { return false; } boolean result = false; File file = new File(path); if (file.exists()) { result = file.delete(); } return result; } public static String getPathFromUri(Uri uri) { String path = ""; try { int sdkVersion = Build.VERSION.SDK_INT; if (sdkVersion >= 19) { path = getPathByCopyFile(TUILogin.getAppContext(), uri); } else { path = getRealFilePath(uri); } } catch (Exception e) { e.printStackTrace(); } if (path == null) { path = ""; } return path; } public static String getRealFilePath(Uri uri) { if (null == uri) { return null; } final String scheme = uri.getScheme(); String data = null; if (scheme == null) { data = uri.getPath(); } else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = TUILogin.getAppContext().getContentResolver().query(uri, new String[] {MediaStore.Images.ImageColumns.DATA}, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; } public static Uri getUriFromPath(String path) { try { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return FileProvider.getUriForFile( TUIConfig.getAppContext(), TUIConfig.getAppContext().getApplicationInfo().packageName + FILE_PROVIDER_AUTH, new File(path)); } else { return Uri.fromFile(new File(path)); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 专为Android4.4以上设计的从Uri获取文件路径 * * Get file path from Uri specially designed for Android4.4 and above */ public static String getPath(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } else { return getPathByCopyFile(context, uri); } } // DownloadsProvider else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); if (id.startsWith("raw:")) { final String path = id.replaceFirst("raw:", ""); return path; } String[] contentUriPrefixesToTry = new String[] {"content://downloads/public_downloads", "content://downloads/my_downloads", "content://downloads/all_downloads"}; for (String contentUriPrefix : contentUriPrefixesToTry) { Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.parseLong(id)); try { String path = getDataColumn(context, contentUri, null, null); if (path != null && Build.VERSION.SDK_INT < 29) { return path; } } catch (Exception e) { e.printStackTrace(); } } // 在某些android8+的手机上,无法获取路径,所以用拷贝的方式,获取新文件名,然后把文件发出去 // On some android8+ mobile phones, the path cannot be obtained, so the new file name is obtained by copying, and then the file is sent out return getPathByCopyFile(context, uri); } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] {split[1]}; String path = getDataColumn(context, contentUri, selection, selectionArgs); if (TextUtils.isEmpty(path) || Build.VERSION.SDK_INT >= 29) { path = getPathByCopyFile(context, uri); } return path; } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { String path = getDataColumn(context, uri, null, null); if (TextUtils.isEmpty(path) || Build.VERSION.SDK_INT >= 29) { // 在某些华为android9+的手机上,无法获取路径,所以用拷贝的方式,获取新文件名,然后把文件发出去 path = getPathByCopyFile(context, uri); } return path; } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } private static String getPathByCopyFile(Context context, Uri uri) { String fileName = getFileName(context, uri); File cacheDir = getDocumentCacheDir(context); File file = generateFileName(fileName, cacheDir); String destinationPath = null; if (file != null) { destinationPath = file.getAbsolutePath(); boolean saveSuccess = saveFileFromUri(context, uri, destinationPath); if (!saveSuccess) { file.delete(); return null; } } return destinationPath; } @Nullable public static File generateFileName(@Nullable String name, File directory) { if (name == null) { return null; } File file = new File(directory, name); if (file.exists()) { String fileName = name; String extension = ""; int dotIndex = name.lastIndexOf('.'); if (dotIndex > 0) { fileName = name.substring(0, dotIndex); extension = name.substring(dotIndex); } int index = 0; while (file.exists()) { index++; name = fileName + '(' + index + ')' + extension; file = new File(directory, name); } } try { if (!file.createNewFile()) { return null; } } catch (IOException e) { e.printStackTrace(); return null; } return file; } public static String getFileName(@NonNull Context context, Uri uri) { String mimeType = context.getContentResolver().getType(uri); String filename = null; if (mimeType == null && context != null) { filename = getName(uri.toString()); } else { Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null); if (returnCursor != null) { int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); returnCursor.moveToFirst(); filename = returnCursor.getString(nameIndex); returnCursor.close(); } } return filename; } public static String getName(String filePath) { if (filePath == null) { return null; } int index = filePath.lastIndexOf('/'); return filePath.substring(index + 1); } public static File getDocumentCacheDir(@NonNull Context context) { File dir = new File(context.getCacheDir(), DOCUMENTS_DIR); if (!dir.exists()) { dir.mkdirs(); } return dir; } private static boolean saveFileFromUri(Context context, Uri uri, String destinationPath) { InputStream is = null; BufferedOutputStream bos = null; try { is = context.getContentResolver().openInputStream(uri); bos = new BufferedOutputStream(new FileOutputStream(destinationPath, false)); byte[] buf = new byte[1024]; int actualBytes; while ((actualBytes = is.read(buf)) != -1) { bos.write(buf, 0, actualBytes); } } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (is != null) { is.close(); } if (bos != null) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context The context. * @param uri The Uri to query. * @param selection (Optional) Filter used in the query. * @param selectionArgs (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } /** * 转换文件大小 * * Convert file size to string * * @param fileS * @return */ public static String formatFileSize(long fileS) { DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("#.00", symbols); String fileSizeString = ""; String wrongSize = "0B"; if (fileS == 0) { return wrongSize; } if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "KB"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "GB"; } return fileSizeString; } // 修复 android.webkit.MimeTypeMap 的 getFileExtensionFromUrl 方法不支持中文的问题 // fix the problem that getFileExtensionFromUrl does not support Chinese public static String getFileExtensionFromUrl(String url) { if (!TextUtils.isEmpty(url)) { int fragment = url.lastIndexOf('#'); if (fragment > 0) { url = url.substring(0, fragment); } int query = url.lastIndexOf('?'); if (query > 0) { url = url.substring(0, query); } int filenamePos = url.lastIndexOf('/'); String filename = 0 <= filenamePos ? url.substring(filenamePos + 1) : url; // if the filename contains special characters, we don't // consider it valid for our matching purposes: // 去掉正则表达式判断以添加中文支持 // if (!filename.isEmpty() && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) if (!filename.isEmpty()) { int dotPos = filename.lastIndexOf('.'); if (0 <= dotPos) { return filename.substring(dotPos + 1).toLowerCase(); } } } return ""; } public static void openFile(String path, String fileName) { Uri uri = FileProvider.getUriForFile( TUIConfig.getAppContext(), TUIConfig.getAppContext().getApplicationInfo().packageName + FILE_PROVIDER_AUTH, new File(path)); if (uri == null) { Log.e("FileUtil", "openFile failed , uri is null"); return; } String fileExtension; if (TextUtils.isEmpty(fileName)) { fileExtension = FileUtil.getFileExtensionFromUrl(path); } else { fileExtension = FileUtil.getFileExtensionFromUrl(fileName); } String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(uri, mimeType); try { Intent chooserIntent = Intent.createChooser(intent, TUIConfig.getAppContext().getString(R.string.open_file_tips)); chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); TUIConfig.getAppContext().startActivity(chooserIntent); } catch (Exception e) { Log.e("FileUtil", "openFile failed , " + e.getMessage()); } } public static long getFileSize(String path) { File file = new File(path); if (file.exists()) { return file.length(); } return 0; } public static String generateImageFilePath() { String name = System.nanoTime() + "_" + Math.abs(new Random().nextInt()); return TUIConfig.getImageBaseDir() + name + ".jpg"; } public static String generateExternalStorageImageFilePath() { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + TUIConfig.getAppContext().getPackageName() + TUIConfig.IMAGE_BASE_DIR_SUFFIX); if (!dir.exists()) { dir.mkdirs(); } return dir.getAbsolutePath() + File.separatorChar + System.nanoTime() + "_" + Math.abs(new Random().nextInt()) + ".jpg"; } public static String generateVideoFilePath() { String name = System.nanoTime() + "_" + Math.abs(new Random().nextInt()); return TUIConfig.getVideoBaseDir() + name + ".mp4"; } public static String generateExternalStorageVideoFilePath() { File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + TUIConfig.getAppContext().getPackageName() + TUIConfig.VIDEO_BASE_DIR_SUFFIX); if (!dir.exists()) { dir.mkdirs(); } return dir.getAbsolutePath() + File.separatorChar + System.nanoTime() + "_" + Math.abs(new Random().nextInt()) + ".mp4"; } public static boolean saveBitmap(String path, Bitmap b) { try { FileOutputStream fout = new FileOutputStream(path); BufferedOutputStream bos = new BufferedOutputStream(fout); b.compress(Bitmap.CompressFormat.JPEG, 100, bos); bos.flush(); bos.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } public static boolean isFileExists(String path) { try { File file = new File(path); return file.exists() && file.isFile(); } catch (Exception e) { return false; } } public static boolean isDirExists(String path) { try { File file = new File(path); return file.exists() && file.isDirectory(); } catch (Exception e) { return false; } } public static boolean isFileSizeExceedsLimit(Uri data, int maxSize) { try { Cursor returnCursor = ServiceInitializer.getAppContext().getContentResolver().query(data, null, null, null, null); if (returnCursor != null) { int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE); returnCursor.moveToFirst(); int size = returnCursor.getInt(sizeIndex); if (size > maxSize) { return true; } returnCursor.close(); return false; } return false; } catch (Exception e) { return false; } } } ``` ``` /*******************************HQW系统文件选择********************************/ /** * HQW系统文件选择 */ public void HQWFileSelect(HQWFileListener hqwFileListener) { this.hqwFileListener = hqwFileListener; fileLauncher.launch(""); } /** * 获取文件 */ ActivityResultLauncher fileLauncher = registerForActivityResult(new ActivityResultContract<String, Uri>() { @Override public Uri parseResult(int i, @Nullable Intent intent) { if (intent!=null){ if (intent.getData()!=null){ return intent.getData(); }else { return null; } }else { return null; } } @NonNull @Override public Intent createIntent(@NonNull Context context, String s) { return new Intent(Intent.ACTION_OPEN_DOCUMENT) .setType("*/*"); } }, new ActivityResultCallback<Uri>() { @Override public void onActivityResult(Uri uri) { if (uri!=null){ hqwFileListener.file(uri); String path= HQWFileUtil.UritoFilePath(HQWAppCompatActivity.this,uri); if (!TextUtils.isEmpty(path)){ hqwFileListener.file(path); } } } }); ```
评论 (
0
)
黎明白昼
创建了
任务
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
分支 (2)
标签 (38)
master
demo
1.9.9
1.9.8
1.9.7
1.9.6
1.9.5
1.9.0
1.6.8
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.9
1.5.8
1.5.7
1.5.1
1.4.1
1.4.0
Beta1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.1.7
1.1.6
1.1.5
1.1.2
1.1.1
1.0.9
1.0.8
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
Android
1
https://gitee.com/BAILIS/HQW.git
[email protected]
:BAILIS/HQW.git
BAILIS
HQW
HQW
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册