Android 文件处理
2021-03-12 本文已影响0人
wenju
获取文件类型并调用系统应用打开相应文件
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
public class FileUtil {
/**
* 获取文件类型
* (获取图片jpg、png等格式、文件word、xml等格式等)
* @param filePath
* @return
*/
public static String getFileType(String filePath) {
HashMap<String, String> mFileTypes = new HashMap<String, String>();
mFileTypes.put("FFD8FF", "jpg");
mFileTypes.put("89504E47", "png");
mFileTypes.put("89504E", "png");
mFileTypes.put("47494638", "gif");
mFileTypes.put("49492A00", "tif");
mFileTypes.put("424D", "bmp");
mFileTypes.put("41433130", "dwg"); //CAD
mFileTypes.put("38425053", "psd");
mFileTypes.put("7B5C727466", "rtf"); //日记本
mFileTypes.put("3C3F786D6C", "xml");
mFileTypes.put("68746D6C3E", "html");
mFileTypes.put("44656C69766572792D646174653A", "eml"); //邮件
mFileTypes.put("D0CF11E0", "doc");
mFileTypes.put("5374616E64617264204A", "mdb");
mFileTypes.put("252150532D41646F6265", "ps");
mFileTypes.put("255044462D312E", "pdf");
mFileTypes.put("504B0304", "zip");
mFileTypes.put("504B03", "apk");
mFileTypes.put("52617221", "rar");
mFileTypes.put("57415645", "wav");
mFileTypes.put("41564920", "avi");
mFileTypes.put("2E524D46", "rm");
mFileTypes.put("000001BA", "mpg");
mFileTypes.put("000001B3", "mpg");
mFileTypes.put("6D6F6F76", "mov");
mFileTypes.put("3026B2758E66CF11", "asf");
mFileTypes.put("4D546864", "mid");
mFileTypes.put("1F8B08", "gz");
mFileTypes.put("000000", "mp4");
if (getFileHeader(filePath) != null) {
if(mFileTypes.get(getFileHeader(filePath))!=null){
return mFileTypes.get(getFileHeader(filePath));
}else{
return "unknown";
}
return mFileTypes.get(getFileHeader(filePath));
} else {
return "";
}
}
/**
* 获取文件头信息
*
* @param filePath
* @return
*/
public static String getFileHeader(String filePath) {
FileInputStream is = null;
String value = null;
try {
is = new FileInputStream(filePath);
byte[] b = new byte[3];
is.read(b, 0, b.length);
value = bytesToHexString(b);
} catch (Exception e) {
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
}
}
}
return value;
}
/**
* 将byte字节转换为十六进制字符串
*
* @param src
* @return
*/
private static String bytesToHexString(byte[] src) {
StringBuilder builder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
String hv;
for (int i = 0; i < src.length; i++) {
hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
if (hv.length() < 2) {
builder.append(0);
}
builder.append(hv);
}
return builder.toString();
}
public static void openFile(Context context, String filePath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filePath);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//android 7.0及以上记得添加
Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", file);
switch (getFileType(filePath)) {
case "mp4":
//已测试,可行
intent.setDataAndType(contentUri, "video/*");
break;
case "jpg":
case "png":
case "gif":
intent.setDataAndType(contentUri, "image/*");
break;
case "pdf":
intent.setDataAndType(contentUri, "application/pdf");
break;
case "apk":
//已测试,可行
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
break;
case "unknown":
default:
intent.setDataAndType(contentUri, "*/*");
break;
}
//如果判断包含文件类型会调用系统默认应用,不包含,弹出选择弹窗自己选择
//注意:不要使用startActivity(intent),测试过很多情况下跳转失败;
context.startActivity(Intent.createChooser(intent, "Select browse tool"));
}
}
附,相应文件类型跳转type
//默认跳转
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.addCategory(Intent.CATEGORY_DEFAULT);
//打开图片文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"image/*");
//打开PDF文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/pdf");
//打开文本文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"text/plain");
//打开音频文件
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot",0);
intent.putExtra("configchange",0);
intent.setDataAndType(uri,"audio/*");
//打开视频文件
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot",0);
intent.putExtra("configchange",0);
intent.setDataAndType(uri,"video/*");
//打开CHM文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/x-chm");
//打开apk文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/vnd.android.package-archive");
//打开PPT文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/vnd.ms-powerpoint");
//打开Excel文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/vnd.ms-excel");
//打开Word文件
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(uri,"application/msword");