uni-app调用原生的文件系统管理器(可选取附件上传)

时间:2023-07-02 13:30:38
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#btn{
display: block;
width: 90%;
height: 50px;
margin: 0px auto;
}
</style>
</head>
<body>
<button id="btn" type="button" class="mui-btn mui-btn-blue mui-btn-block">按钮</button>
<script type="text/javascript">
document.querySelector('#btn').addEventListener('click',function () {
var REQUESTCODE = 1;
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*");//设置类型,任意类型
//intent.setType("image/*");
//intent.setType("audio/*"); //选择音频
//intent.setType("video/*"); //选择视频 (mp4 3gp 是android支持的视频格式) intent.addCategory(Intent.CATEGORY_OPENABLE);
main.startActivityForResult(intent,REQUESTCODE); main.onActivityResult = function(requestCode, resultCode, data) {
if (REQUESTCODE == requestCode) {
var context = main;
plus.android.importClass(data);
// 获得文件路径
var fileData = data.getData();
var path = plus.android.invoke(fileData, "getPath");
console.log("path:"+path); // 判断文件类型
var resolver = context.getContentResolver();
var fileType = plus.android.invoke(resolver, "getType",fileData);
console.log("fileType:"+fileType);
}
}
})
</script>
</body>
</html>

后来发现获取的地址uri是编码过得,我要获取本地的路径下面是完整版的

//调用原生文件系统管理器并选取文件获取文件地址
window.PickFile = function(callback, acceptType) {//acceptType为你要查的文件类型"image/*","audio/*","video/*;image/*"  // intent.setType("image/*");//intent.setType("audio/*"); //选择音频//intent.setType("video/*;image/*"); //选择视频 (mp4 3gp 是android支持的视频格式)
var CODE_REQUEST = 1000;
var main = plus.android.runtimeMainActivity();
if(plus.os.name == 'Android') {
var Intent = plus.android.importClass('android.content.Intent');
var intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if(acceptType) {
intent.setType(acceptType);
} else {
intent.setType("*/*");
}
main.onActivityResult = function(requestCode, resultCode, data) {
if(requestCode == CODE_REQUEST) {
var uri = data.getData();
plus.android.importClass(uri);
var Build = plus.android.importClass('android.os.Build');
var isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; var DocumentsContract = plus.android.importClass('android.provider.DocumentsContract');
// DocumentProvider
if(isKitKat && DocumentsContract.isDocumentUri(main, uri)) {
console.log("版本大于 4.4 ");
// ExternalStorageProvider
if("com.android.externalstorage.documents" == uri.getAuthority()) {
var docId = DocumentsContract.getDocumentId(uri);
var split = docId.split(":");
var type = split[0]; if("primary" == type) {
var Environment = plus.android.importClass('android.os.Environment');
callback(Environment.getExternalStorageDirectory() + "/" + split[1]);
} else {
var System = plus.android.importClass('java.lang.System');
var sdPath = System.getenv("SECONDARY_STORAGE");
if(sdPath) {
callback(sdPath + "/" + split[1]);
}
}
}
// DownloadsProvider
else if("com.android.providers.downloads.documents" == uri.getAuthority()) {
var id = DocumentsContract.getDocumentId(uri);
var ContentUris = plus.android.importClass('android.content.ContentUris');
var contentUri = ContentUris.withAppendedId(
// Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Uri.parse("content://downloads/public_downloads"), id);
callback(getDataColumn(main, contentUri, null, null));
}
// MediaProvider
else if("com.android.providers.media.documents" == uri.getAuthority()) {
var docId = DocumentsContract.getDocumentId(uri);
var split = docId.split(":");
var type = split[0]; var MediaStore = plus.android.importClass('android.provider.MediaStore');
if("image" == type) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if("video" == type) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if("audio" == type) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
} var selection = "_id=?";
var selectionArgs = new Array();
selectionArgs[0] = split[1]; callback(getDataColumn(main, contentUri, selection, selectionArgs));
}
}
// MediaStore (and general)
else if("content" == uri.getScheme()) {
callback(getDataColumn(main, uri, null, null));
}
// File
else if("file" == uri.getScheme()) {
callback(uri.getPath());
}
}
}
main.startActivityForResult(intent, CODE_REQUEST);
}
} function getDataColumn(main, uri, selection, selectionArgs) {
plus.android.importClass(main.getContentResolver());
let cursor = main.getContentResolver().query(uri, ['_data'], selection, selectionArgs,
null);
plus.android.importClass(cursor);
if(cursor != null && cursor.moveToFirst()) {
var column_index = cursor.getColumnIndexOrThrow('_data');
var result = cursor.getString(column_index)
cursor.close();
return result;
}
return null;
}