Android_磊子
Android_磊子
  • 发布:2017-04-01 18:53
  • 更新:2022-12-14 08:40
  • 阅读:10454

如何通过Native.js进行文件选取

分类:Native.js

5+ API中plus.gallery.pick只能选取图片或是视频文件,那问题来了,其他类型文件如何选取。
此时就该发挥Native.js的强大之处了,如果会安卓原生编程,代码so easy。不会的同学你可以选择找到代码copy,会是百度原生代码实现然后转换为Native.js语法。废话不多说,反正你可以找到很多方式配合5+API完成自己的app。
开吃。。。

window.PickFile = function(callback,acceptType){  
    function ip(obj){  
        plus.android.importClass(obj);  
        return obj;  
    }  
    if(plus.os.name == 'Android' && typeof callback == 'function'){  
        var CODE_REQUEST = 1000;  
        var context = plus.android.runtimeMainActivity();  
        ip(context);  
        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("*/*");  
        }  
        context.onActivityResult = function(requestCode,resultCode,intentData){  
            if(requestCode == CODE_REQUEST){  
                if(intentData){  
                    var uriValue = intentData.getData();  
                    plus.android.importClass(uriValue);  
                    var scheme = uriValue.getScheme();  
                    if(scheme == 'content'){//还需要进行数据库查询,一般图片数据  
                        var cursor = ip(context.getContentResolver()).query(uriValue,['_data'], null, null, null);  
                        if(cursor){  
                            ip(cursor).moveToFirst();  
                            var columnIndex = cursor.getColumnIndex('_data');  
                            picturePath = cursor.getString(columnIndex);  
                            cursor.close();  
                            callback(picturePath);//返回文件路径  
                        }  
                    }else if(scheme == 'file'){  
                        callback(uriValue.getPath());//返回文件路径  
                    }  
                }else{  
                    callback(null);  
                }  
            }  
        }  
        context.startActivityForResult(intent,CODE_REQUEST);  
    }  
}
4 关注 分享
Trust x***@qq.com BoredApe brush

要回复文章请先登录注册

小资电脑

小资电脑

虽然不懂原生,赞一个
2017-04-02 19:56