点击按钮进入系统文件管理器
Intent intent =new Intent(Intent.ACTION_GET_CONTENT);
//intent.setType(“image/*”);//选择图片
//intent.setType(“audio/*”); //选择音频
//intent.setType(“video/*”); //选择视频 (mp4 3gp 是android支持的视频格式)
//intent.setType(“video/*;image/*”);//同时选择视频和图片
// intent.setType("*/*");//无类型限制
intent.setType("application/zip");//zip
// intent.setType("application/x-rar-compressed");//rar
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent,RECORD_CLECK_CODE);
根据自己所需过滤文件类型
onActivityResult中接收结果
if (requestCode ==RECORD_CLECK_CODE && resultCode ==RESULT_OK) {
Uri uri = data.getData();
if ("file".equalsIgnoreCase(uri.getScheme())){//使用第三方应用打开
path =uri.getPath();
tv_zip_url.setText(path);
Toast.makeText(this,path+"11111",Toast.LENGTH_SHORT).show();
return;
}
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.KITKAT) {//4.4以后
// path = getPath(this, uri);//我的荣耀20拿不到,直接用了uri
// String path = getPath(this, uri);
uritofileandroidQ = uriToFileApiQ(uri);
tv_zip_url.setText(uritofileandroidQ +"");
Toast.makeText(this,"file"+uritofileandroidQ,Toast.LENGTH_SHORT).show();
}else {//4.4以下下系统调用方法
path = getRealPathFromURI(uri);
tv_zip_url.setText(path);
Toast.makeText(MainActivity.this,path+"222222",Toast.LENGTH_SHORT).show();
}
由于权限问题需要将uri转为file
@RequiresApi(api =Build.VERSION_CODES.Q)
public File uriToFileApiQ(Uri uri) {
File file =null;
//android10以上转换
if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
file =new File(uri.getPath());
}else if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//把文件复制到沙盒目录
ContentResolver contentResolver =this.getContentResolver();
Cursor cursor =contentResolver.query(uri,null,null,null,null);
if (cursor.moveToFirst()) {
String displayName =cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
try {
InputStream is =contentResolver.openInputStream(uri);
File cache =new File(this.getExternalCacheDir().getAbsolutePath(),Math.round((Math.random() +1) *1000) +displayName);
FileOutputStream fos =new FileOutputStream(cache);
FileUtils.copy(is,fos);
file =cache;
fos.close();
is.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
public String getRealPathFromURI(Uri contentUri) {
String res =null;
String[]proj = {MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri,proj,null,null,null);
if(null!=cursor&&cursor.moveToFirst()){;
int column_index =cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res =cursor.getString(column_index);
cursor.close();
}
return res;
}
Android4.4以下
public String getRealPathFromURI(Uri contentUri) {
String res =null;
String[]proj = {MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri,proj,null,null,null);
if(null!=cursor&&cursor.moveToFirst()){;
int column_index =cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res =cursor.getString(column_index);
cursor.close();
}
return res;
}
读取file放入byte[]中
private byte[]readFileToByteArray(String path) {
File file =new File(path);
FileInputStream in =null;
if(!file.exists()) {
Log.e(TAG,"File doesn't exist!");
return null;
}
try {
in =new FileInputStream(file);
long inSize = in.getChannel().size();//判断FileInputStream中是否有内容
if (inSize ==0) {
Log.d(TAG,"The FileInputStream has no content!");
return null;
}
byte[]buffer =new byte[in.available()];//in.available() 表示要读取的文件中的数据长度
in.read(buffer);//将文件中的数据读到buffer中
return buffer;
}catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}catch (IOException e) {
e.printStackTrace();
return null;
}finally {
try {
in.close();
}catch (IOException e) {
return null;
}
//或IoUtils.closeQuietly(in);
}
}
这样就OK了!