Android10 通過系統(tǒng)文件管理器拿到zip文件uri路徑,uri轉(zhuǎn)換為file,讀取file文件到byte[]中

點(diǎn)擊按鈕進(jìn)入系統(tǒng)文件管理器

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);

根據(jù)自己所需過濾文件類型

onActivityResult中接收結(jié)果

if (requestCode ==RECORD_CLECK_CODE && resultCode ==RESULT_OK) {

Uri uri = data.getData();

if ("file".equalsIgnoreCase(uri.getScheme())){//使用第三方應(yīng)用打開

? ? ? ? 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以下下系統(tǒng)調(diào)用方法

? ? ? ? path = getRealPathFromURI(uri);

tv_zip_url.setText(path);

Toast.makeText(MainActivity.this,path+"222222",Toast.LENGTH_SHORT).show();

}

由于權(quán)限問題需要將uri轉(zhuǎn)為file

@RequiresApi(api =Build.VERSION_CODES.Q)

public File uriToFileApiQ(Uri uri) {

File file =null;

//android10以上轉(zhuǎn)換

? ? if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {

file =new File(uri.getPath());

}else if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {

//把文件復(fù)制到沙盒目錄

? ? ? ? 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中是否有內(nèi)容

? ? ? ? if (inSize ==0) {

Log.d(TAG,"The FileInputStream has no content!");

return null;

}

byte[]buffer =new byte[in.available()];//in.available() 表示要讀取的文件中的數(shù)據(jù)長度

? ? ? ? in.read(buffer);//將文件中的數(shù)據(jù)讀到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了!



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容