uni-app,安卓本地文件选择,返回路径
最近在开发一个app,选取本地文件上传安卓选择本地文件时,需要返回文件路径,然后调用uni-app的上传接口去上传文件。首先是要选择文件,并返回文件路径// chooseFile.js文件内容function chooseFile(callback, acceptType) {//acceptType为你要查的文件类型"image/*","audio/*","video/*;image/*"//
·
最近在开发一个app,选取本地文件上传
安卓选择本地文件时,需要返回文件路径,然后调用uni-app的上传接口去上传文件。首先是要选择文件,并返回文件路径
// chooseFile.js文件内容
function chooseFile(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;
}
export default chooseFile
vue页面内容
<template>
<view class="s_page">
<view class="container mb40">
<view class="form-item flex_ac mb30">
<view class="title f24_999">播报名称</view>
<view class="flex_ac input-con">
<input class="f24_333 flex-1" type="text" placeholder="请输入播报名称">
</view>
</view>
<view class="form-item mb30 flex">
<view class="title f24_999">播报内容</view>
<view class="input-con area">
<textarea class="f24_333" maxlength="150" placeholder="请输入备注信息(150字以内)" />
</view>
</view>
<view class="form-item flex_ac mb30">
<view class="title f24_999">播报语音</view>
<view id="btn" @tap.stop="addFile" class="btn f24_49C"
style="opacity: 0;"
>选择文件</view>
<view class=" flex mr30">
<image class="audio" src="/static/image/audio.png" mode=""></image>
<view>
<view class="f24_666 mb10">关闭电梯.mp3</view>
<view class="f20_999">00:53</view>
</view>
</view>
<text class="f24_49C">重新选择</text>
</view>
</view>
<view class="confirm-btn">添加方案</view>
</view>
</template>
<script>
import axios from '@/api/axios.js';
import chooseFile from '@/utils/chooseFile.js';
export default {
data() {
return {
id:'',
report_name:'',
content:'',
voice_link_id:'',
CustomBar:this.CustomBar
};
},
onLoad({id}){
this.id = id;
},
mounted(){
},
methods:{
addFile(){
chooseFile((res)=>{
axios.uploadImg('file://'+res).then(res=>{
console.log(res);
});
},"audio/*")
},
}
}
</script>
<style scoped lang="scss">
.s_page{
padding: 20rpx 30rpx;
.container{
background-color: #FFFFFF;
border-radius:10rpx;
padding: 20rpx;
.form-item{
.title{
width: 120rpx;
flex: 0 0 120rpx;
}
.audio{
width: 44rpx;
height: 56rpx;
margin-right: 20rpx;
}
.btn{
@include flex_abc;
width:140rpx;
height:64rpx;
border-radius:32rpx;
border:2rpx solid rgba(73,205,223,1);
}
.input-con{
height:80rpx;
background:rgba(247,247,247,1);
border-radius:10rpx;
width: 100%;
padding:0 20rpx;
&.area{
height:200rpx;
background:rgba(247,247,247,1);
border-radius:10rpx;
padding: 16rpx 20rpx;
textarea{
width: 100%;
height: 100%;
display: block;
}
}
}
}
}
}
</style>
//图片上传方法
重点还是在这张图,返回的路径必须前面带上file://,不然上传失败
转载https://www.cnblogs.com/lizhao123/p/9951581.html
更多推荐
已为社区贡献3条内容
所有评论(0)