https://www.doooor.com/thread-15324-1.html
设置多种文件类型
通过intent.setType()方式设置的文件类型,只能生效一次,所以如果想只选择doc、excel、pdt、ppt等办公文档,过滤掉其他文件,就不能使用intent .setType()方式,而是使用Intent.EXTRA_MIME_TYPES来传值。
final String DOC = "application/msword";
final String XLS = "application/vnd.ms-excel";
final String PPT = "application/vnd.ms-powerpoint";
final String DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
final String XLSX = "application/x-excel";
final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
final String PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
final String PDF = "application/pdf";
final String ZIP = "application/zip";
final String ZIP7 = "application/x-7z-compressed";
final String RAR = "application/x-rar-compressed";
/**
* 多种文件类型
*/
private void chooseMoreTypes() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimeTypes = {DOC, DOCX, PDF, PPT, PPTX, XLS, XLS1, XLSX,RAR,ZIP,ZIP7};
intent.setType("application/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_CODE_FILE);
}
压缩格式为 application/zip application/x-7z-compressed application/x-rar-compressed 但是在 有的手机 不能选择 rar 7z 置灰色 无法选择
查看了代码 和测试 没有写错地方
猜测可能是 minitye 不对
Android代码中导出的MiniType类型以及对应的扩展名
https://blog.csdn.net/aichixihongshi19/article/details/83939589
网上公布的minitype 在手机上可能不对 如何获取minitype呢?
方法1:
import java.util.Locale;
private static String getSuffix(File file) {
if (file == null || !file.exists() || file.isDirectory()) {
return null;
}
String fileName = file.getName();
if (fileName.equals("") || fileName.endsWith(".")) {
return null;
}
int index = fileName.lastIndexOf(".");
if (index != -1) {
return fileName.substring(index + 1).toLowerCase(Locale.US);
} else {
return null;
}
}
public static String getMimeType(File file){
String suffix = getSuffix(file);
if (suffix == null) {
return "file/*";
}
String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);
if (type != null || !type.isEmpty()) {
return type;
}
return "file/*";
}
方法2:
public static String getMimeType(String filePath) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
String mime = "text/plain";
if (filePath != null) {
try {
mmr.setDataSource(filePath);
mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
} catch (IllegalStateException e) {
return mime;
} catch (IllegalArgumentException e) {
return mime;
} catch (RuntimeException e) {
return mime;
}
}
return mime;
}
上面都是通过文件路径获取 现在android选择文件系统 返回的都是 URI
通过 Uri获取 文件的minitype
public static String getMimeType(Context context,Uri uri) {
ContentResolver cR = context.getContentResolver();
MimeTypeMap mime = MimeTypeMap.getSingleton();
//String type = mime.getExtensionFromMimeType(cR.getType(uri));
String type = cR.getType(uri);
return type;
}
返回 7z 在 nexus 5s 8.0系统 返回的 application/octet-stream 所有在这个手机 设置为application/x-7z-compressed 无法读取 7z 文件
* zip application/zip
* 7z application/octet-stream application/x-7z-compressed
* rar application/rar
3.找到问题所在 那minitype 跟什么有关系呢?
从网上找的 两个截图
image.png image.png image.png
网友评论