nativejs调用android原生文件管理
按钮
document.querySelector('#btn').addEventListener('click',function () {
var REQUESTCODE = 1;
var main = plus.android.runtimeMainActivity();
var Intent = plus.android.importClass('android.content.Intent');
var MediaStore = plus.android.importClass("android.provider.MediaStore");
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);
// http://stackoverflow.com/questions/3401579/get-filename-and-path-from-uri-from-mediastore
main.onActivityResult = function(requestCode, resultCode, data) {
if (REQUESTCODE == requestCode) {
var context = main;
plus.android.importClass(data);
// 获得文件URI路径 (content://开头的uri)
var uri = data.getData();
plus.android.importClass(uri);
// 获取管理所有程序的实例
var resolver = context.getContentResolver();
plus.android.importClass(resolver);
// 判断文件类型
var fileType =resolver.getType(uri);
console.log("fileType:"+fileType);
try{
var scheme = uri.getScheme();
if (scheme != null && scheme === "content") {
// 获取实例
var cursor = resolver.query(uri, null, null, null, null);
plus.android.importClass(cursor);
var columnIndex;
if (cursor != null && cursor.moveToFirst()) {
if(fileType.indexOf('image')>-1){ // 图片
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
} else if(fileType.indexOf('audio')>-1) { // 音频
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
} else if(fileType.indexOf('video')>-1) { // 视频
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
}
// 取出文件路径
var filePath = cursor.getString(columnIndex);
console.log("filePath:"+filePath);
// 解析文件的属性
plus.io.resolveLocalFileSystemURL(filePath,function(entry) {
entry.file(function(file){
console.log("size=="+file.size);
console.log("name=="+file.name);
});
}, function (e) {
console.log("Resolve file URL failed: " + e.message);
});
}
// 关闭游标
cursor.close();
}
}catch(e){
}
}
}
})
自定义录制视频:
按钮
document.querySelector('#btn').addEventListener('click',function () {
// 调用原生android摄像头
var VIDEOZOOM = 200;
var MediaStore = plus.android.importClass("android.provider.MediaStore");
var Intent = plus.android.importClass("android.content.Intent");
// 导入后可以使用new方法创建类的示例对象
var intent = new Intent("android.media.action.VIDEO_CAPTURE");
intent.putExtra("android.intent.extra.videoQuality", 1);//0 means low quality, 1 means high quality
//intent.putExtra("android.provider.MediaStore.EXTRA_OUTPUT", url);
intent.putExtra("android.intent.extra.durationLimit", 3);//设置录像时间
var main = plus.android.runtimeMainActivity();
main.startActivityForResult(intent,VIDEOZOOM);
//获取返回参数
main.onActivityResult = function(requestCode, resultCode, data) {
var context = main;
plus.android.importClass(data);
var uri = data.getData();
var resolver = context.getContentResolver();
plus.android.importClass(resolver);
var cursor = resolver.query(uri, null, null, null, null);
plus.android.importClass(cursor);
cursor.moveToFirst();
var column = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
// 获取录制的视频路径
var filePath = cursor.getString(column);
// 解析视频文件的属性
plus.io.resolveLocalFileSystemURL(filePath,function(entry) {
entry.file(function(file){
console.log("size=="+file.size);
console.log("name=="+file.name);
});
}, function (e) {
console.log("Resolve file URL failed: " + e.message);
});
};
})