version:1.9.1

fix:
update:个人信息页面完善
This commit is contained in:
2024-09-14 09:40:25 +08:00
parent 58b3e18727
commit 2c4af66ad8
156 changed files with 4819 additions and 1731 deletions

View File

@@ -0,0 +1,221 @@
package com.uiui.zyos.utils;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import androidx.annotation.RequiresApi;
import androidx.core.content.FileProvider;
import com.uiui.zyos.BuildConfig;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
public class OpenFileUtil {
public static void openFile(Context context, String filePath) {
File file = new File(filePath);
openFile(context, file);
}
public static void openFile(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uriForFile;
if (Build.VERSION.SDK_INT > 23) {
//Android 7.0之后
uriForFile = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".FileProvider", file);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);//给目标文件临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//给目标文件临时授权
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
uriForFile = Uri.fromFile(file);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task;
// 若有则在该Task上创建Activity若没有则新建具有该Activity属性的Task并在该新建的Task上创建Activity。
intent.setDataAndType(uriForFile, getMimeTypeFromFile(file));
context.startActivity(intent);
}
/**
* 使用自定义方法获得文件的MIME类型
*/
private static String getMimeTypeFromFile(File file) {
String type = "*/*";
String fName = file.getName();
//获取后缀名前的分隔符"."在fName中的位置。
int dotIndex = fName.lastIndexOf(".");
if (dotIndex > 0) {
//获取文件的后缀名
String end = fName.substring(dotIndex, fName.length()).toLowerCase(Locale.getDefault());
//在MIME和文件类型的匹配表中找到对应的MIME类型。
HashMap<String, String> map = MyMimeMap.getMimeMap();
if (!TextUtils.isEmpty(end) && map.keySet().contains(end)) {
type = map.get(end);
}
}
return type;
}
public static class MyMimeMap {
private static final HashMap<String, String> mapSimple = new HashMap<>();
/**
* 常用"文件扩展名—MIME类型"匹配表。
* 注意此表并不全也并不是唯一的就像有人喜欢用浏览器打开TXT一样你可以根据自己的爱好自定义。
*/
public static HashMap<String, String> getMimeMap() {
if (mapSimple.size() == 0) {
mapSimple.put(".3gp", "video/3gpp");
mapSimple.put(".apk", "application/vnd.android.package-archive");
mapSimple.put(".asf", "video/x-ms-asf");
mapSimple.put(".avi", "video/x-msvideo");
mapSimple.put(".bin", "application/octet-stream");
mapSimple.put(".bmp", "image/bmp");
mapSimple.put(".c", "text/plain");
mapSimple.put(".chm", "application/x-chm");
mapSimple.put(".class", "application/octet-stream");
mapSimple.put(".conf", "text/plain");
mapSimple.put(".cpp", "text/plain");
mapSimple.put(".doc", "application/msword");
mapSimple.put(".docx", "application/msword");
mapSimple.put(".exe", "application/octet-stream");
mapSimple.put(".gif", "image/gif");
mapSimple.put(".gtar", "application/x-gtar");
mapSimple.put(".gz", "application/x-gzip");
mapSimple.put(".h", "text/plain");
mapSimple.put(".htm", "text/html");
mapSimple.put(".html", "text/html");
mapSimple.put(".jar", "application/java-archive");
mapSimple.put(".java", "text/plain");
mapSimple.put(".jpeg", "image/jpeg");
mapSimple.put(".jpg", "image/jpeg");
mapSimple.put(".js", "application/x-javascript");
mapSimple.put(".log", "text/plain");
mapSimple.put(".m3u", "audio/x-mpegurl");
mapSimple.put(".m4a", "audio/mp4a-latm");
mapSimple.put(".m4b", "audio/mp4a-latm");
mapSimple.put(".m4p", "audio/mp4a-latm");
mapSimple.put(".m4u", "video/vnd.mpegurl");
mapSimple.put(".m4v", "video/x-m4v");
mapSimple.put(".mov", "video/quicktime");
mapSimple.put(".mp2", "audio/x-mpeg");
mapSimple.put(".mp3", "audio/x-mpeg");
mapSimple.put(".mp4", "video/mp4");
mapSimple.put(".mpc", "application/vnd.mpohun.certificate");
mapSimple.put(".mpe", "video/mpeg");
mapSimple.put(".mpeg", "video/mpeg");
mapSimple.put(".mpg", "video/mpeg");
mapSimple.put(".mpg4", "video/mp4");
mapSimple.put(".mpga", "audio/mpeg");
mapSimple.put(".msg", "application/vnd.ms-outlook");
mapSimple.put(".ogg", "audio/ogg");
mapSimple.put(".pdf", "application/pdf");
mapSimple.put(".png", "image/png");
mapSimple.put(".pps", "application/vnd.ms-powerpoint");
mapSimple.put(".ppt", "application/vnd.ms-powerpoint");
mapSimple.put(".pptx", "application/vnd.ms-powerpoint");
mapSimple.put(".prop", "text/plain");
mapSimple.put(".rar", "application/x-rar-compressed");
mapSimple.put(".rc", "text/plain");
mapSimple.put(".rmvb", "audio/x-pn-realaudio");
mapSimple.put(".rtf", "application/rtf");
mapSimple.put(".sh", "text/plain");
mapSimple.put(".tar", "application/x-tar");
mapSimple.put(".tgz", "application/x-compressed");
mapSimple.put(".txt", "text/plain");
mapSimple.put(".wav", "audio/x-wav");
mapSimple.put(".wma", "audio/x-ms-wma");
mapSimple.put(".wmv", "audio/x-ms-wmv");
mapSimple.put(".wps", "application/vnd.ms-works");
mapSimple.put(".xml", "text/plain");
mapSimple.put(".xls", "application/vnd.ms-excel");
mapSimple.put(".xlsx", "application/vnd.ms-excel");
mapSimple.put(".z", "application/x-compress");
mapSimple.put(".zip", "application/zip");
mapSimple.put("", "*/*");
}
return mapSimple;
}
}
public static String getFileExtension(String fileName) {
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex < 0) {
return "";
}
return fileName.substring(dotIndex + 1);
}
// Android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(Context context, String path) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File file = new File(path);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(context, "com.video.stos.FileProvider", file);
intent.setDataAndType(uri, "application/pdf");
} else {
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
}
return intent;
}
public static final String DOWNLOAD_URI = "content://com.android.externalstorage.documents/document/primary:Download/";
// Android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uriForFile;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//Android 7.0之后
// uriForFile = FileProvider.getUriForFile(context, "com.video.stos.FileProvider", file);
uriForFile = getFileUri(context, file.getAbsolutePath());
// intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);//给目标文件临时授权
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//给目标文件临时授权
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
} else {
uriForFile = Uri.fromFile(file);
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task;
Log.e("lhl", "uri+" + uriForFile + "");
//Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uriForFile, "application/pdf");
return intent;
}
@RequiresApi(api = Build.VERSION_CODES.Q)
public static Uri getFileUri(Context context, String filePath) {
Log.e("getFileUri", "getFileUri: " + MediaStore.Downloads.EXTERNAL_CONTENT_URI);
Cursor cursor = context.getContentResolver().query(MediaStore.Downloads.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Downloads._ID}, MediaStore.Downloads.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/downloads");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Downloads.DATA, filePath);
return context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, values);
}
}
}