陈建华的博客
专注web开发
uni-app调用Android文件系统
2020-03-05 17:09:28   阅读203次

最近用 uni-app做多媒体上传功能, 文件选择,视频,图片官方分别有uni.chooseVideo和uni.chooseImage接口,很方便,但是涉及到其他文件类型选择比如音频,就没有现成的接口。 在网上搜索了一圈,终于找到了通过Native.js调用原生文件系统的方法 。 原博客地址:https://www.cnblogs.com/lizhao123/p/9951581.html;
我结合了作者的方法,简单的封装为pickFile.js,调用起来很方便。用法很简单,把该文件放到你的项目JS目录下,通过 import {pickFile} from '@/js/common/pickFile.js' 方式引入,然后

pickFile.PickFile(function(audioSrc){  
     console.log(audioSrc)  
    _this.audioSrc = 'file://' + audioSrc;  
    // _this.audioSrc = 'https://img-cdn-qiniu.dcloud.net.cn/uniapp/audio/music.mp3'  
    // 这里就会得到 你选择的文件路径。拿到路径后,你可以用uni.uploadFile  进行上传。  }, 'audio/*');  
//这里第一个参数是 回调函数,第二个是你要选择的文件类 "image/*","audio/*","video/*;image/*"

以下是我修改后的pickFile.js 源码。

var pickFile = {  
    //调用原生文件系统管理器并选取文件获取文件地址  
    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("*/*");  
            }  
            let _this = pickFile;  
            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(_this.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(_this.getDataColumn(main, contentUri, selection, selectionArgs));  
                        }  
                    }  
                    // MediaStore (and general)  
                    else if ("content" == uri.getScheme()) {  
                        callback(_this.getDataColumn(main, uri, null, null));  
                    }  
                    // File  
                    else if ("file" == uri.getScheme()) {  
                        callback(uri.getPath());  
                    }  
                }  
            }  
            main.startActivityForResult(intent, CODE_REQUEST);  
        }  
    },  
    getDataColumn:function(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;  
    }  
}  
module.exports = {  
    pickFile: pickFile  
}

 



-----------------------------------------------------
转载请注明来源此处
原地址:#

-----网友评论----
暂无评论
-----发表评论----
微网聚博客乐园 ©2014 blog.mn886.net 鲁ICP备14012923号   网站导航