你的城主大人
你的城主大人
  • 发布:2020-08-24 09:44
  • 更新:2020-08-28 10:31
  • 阅读:725

安卓使用Native.js打开相册选取图片后无法获取到,hbuilder可以,但是打包后安卓10上没反应

分类:HTML5+

产品分类: HTML5+

HBuilderX版本号: 2.8.8

手机系统: Android

手机系统版本号: Android 10

手机厂商: 小米

手机机型: MIX2

打包方式: 云端

操作步骤:

选择图片

预期结果:

获取到图片路径显示到页面

实际结果:

安卓10选择后无反应

bug描述:

这段代码安卓8可以,安卓10就不行

        Android: function(callback) {  
            var REQUESTCODE = 1;  
            var main = plus.android.runtimeMainActivity();  
            var IntentCls = plus.android.importClass("android.content.Intent");  
            var MediaStoreCls = plus.android.importClass("android.provider.MediaStore");  
            var intent = new IntentCls(IntentCls.ACTION_PICK, null);  
            intent.setDataAndType(MediaStoreCls.Images.Media.EXTERNAL_CONTENT_URI, "image/*");  
            main.startActivityForResult(intent, REQUESTCODE);  
            main.onActivityResult = function(requestCode, resultCode, data) {  
                if (REQUESTCODE == requestCode && data) {  
                    var context = main;  
                    plus.android.importClass(data);  

                    // 获得文件URI路径 (content://开头的uri)  
                    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);  
                    if (cursor != null && cursor.moveToFirst()) {  
                        // var _id = cursor.getColumnIndexOrThrow(MediaStoreCls.Images.Media._ID);  
                        // var display_name = cursor.getColumnIndexOrThrow(MediaStoreCls.Images.Media.DISPLAY_NAME);  
                        var columnIndex = cursor.getColumnIndexOrThrow(MediaStoreCls.Images.Media.DATA);  
                        // 取出文件路径  
                        var filePath = cursor.getString(columnIndex);  
                        // var pictureInfoCls = plus.android.importClass("android.app.Activity.PictureActivity.PictureInfo");  
                        // var pictureInfo = new pictureInfoCls(cursor.getString(_id), cursor.getString(display_name), filePath);  
                        // 获取图片方向  
                        var exifInterfaceCls = plus.android.importClass("android.media.ExifInterface");  
                        var exifInterface  = new exifInterfaceCls(filePath);  
                        var orientation = exifInterface.getAttributeInt(exifInterfaceCls.TAG_ORIENTATION, exifInterfaceCls.ORIENTATION_NORMAL);  
                        var degree = 0;  
                        switch (orientation) {  
                            case exifInterfaceCls.ORIENTATION_ROTATE_90:  
                                degree = 90;  
                                break;  
                            case exifInterfaceCls.ORIENTATION_ROTATE_180:  
                                degree = 180;  
                                break;  
                            case exifInterfaceCls.ORIENTATION_ROTATE_270:  
                                degree = 270;  
                                break;  
                        }  
                        if(orientation > 0){  
                            var matrixCls = plus.android.importClass("android.graphics.Matrix");  
                            var matrix = new matrixCls();  
                            matrix.postRotate(degree);  
                            var bitmapCls = plus.android.importClass("android.graphics.Bitmap");  
                            var bitmapFactoryCls = plus.android.importClass("android.graphics.BitmapFactory");  
                            var bitmapFactory_Options = new bitmapFactoryCls.Options();  
                            bitmapFactory_Options.inJustDecodeBounds = true;  
                            var currBitmap = bitmapFactoryCls.decodeFile(filePath, bitmapFactory_Options);  
                            var retBitmap = bitmapCls.createBitmap(currBitmap, 0, 0, currBitmap.getWidth(), currBitmap.getHeight(), matrix, true);  
                            var fileCls = plus.android.importClass("java.io.File");  
                            plus.io.resolveLocalFileSystemURL("_doc/", function(gifEntry) {   
                                var fileObj_path = gifEntry.fullPath + "image/";  
                                var fileObj_name = new Date().getTime() + ".png";  
                                var fileObj = new fileCls(fileObj_path, fileObj_name);  
                                var fileOutputStreamCls = plus.android.importClass("java.io.FileOutputStream");  
                                var fileOutputStream = new fileOutputStreamCls(fileObj);  
                                retBitmap.compress(bitmapCls.CompressFormat.PNG, 100, fileOutputStream);  
                                fileOutputStream.flush();  
                                fileOutputStream.close();  
                                filePath = fileObj_path + fileObj_name;  
                                // 执行回调  
                                callback && callback({ret:1,filePath:"file://" + filePath});  
                            });  
                        }else{  
                            // 执行回调  
                            callback && callback({ret:1,filePath:"file://" + filePath});  
                        }  
                    }  
                    // 关闭游标  
                    cursor.close();  
                }else{  
                    // 取消  
                    callback && callback({ret:-1});  
                }  
            }  
        },
2020-08-24 09:44 负责人:无 分享
已邀请:
你的城主大人

你的城主大人 (作者)

离线打包可以用,但是在线打包完没反应,很是无奈。

/**  
 * 选择图片安卓  
 */  
var SelectPicComm = {  
    pick: {  
        Android: function(callback) {  
            var REQUESTCODE = 1;  
            var main = plus.android.runtimeMainActivity();  
            var IntentCls = plus.android.importClass("android.content.Intent");  
            var MediaStoreCls = plus.android.importClass("android.provider.MediaStore");  
            var intent = new IntentCls(IntentCls.ACTION_PICK, null);  
            intent.setDataAndType(MediaStoreCls.Images.Media.EXTERNAL_CONTENT_URI, "image/*");  
            main.startActivityForResult(intent, REQUESTCODE);  
            main.onActivityResult = function(requestCode, resultCode, data) {  
                if (REQUESTCODE == requestCode && data) {  
                    plus.nativeUI.showWaiting();  
                    var context = main;  
                    plus.android.importClass(data);  
                    // 获得文件URI路径 (content://开头的uri)  
                    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);  
                    if (cursor != null && cursor.moveToFirst()) {  
                        var columnIndex = cursor.getColumnIndexOrThrow(MediaStoreCls.Images.Media.DATA);  
                        // 取出文件路径  
                        var filePath = cursor.getString(columnIndex);  
                        // 获取图片方向  
                        var exifInterfaceCls = plus.android.importClass("android.media.ExifInterface");  
                        var exifInterface  = new exifInterfaceCls(filePath);  
                        var orientation = exifInterface.getAttributeInt(exifInterfaceCls.TAG_ORIENTATION, exifInterfaceCls.ORIENTATION_NORMAL);  
                        var degree = 0;  
                        switch (orientation) {  
                            case exifInterfaceCls.ORIENTATION_ROTATE_90:  
                                degree = 90;  
                                break;  
                            case exifInterfaceCls.ORIENTATION_ROTATE_180:  
                                degree = 180;  
                                break;  
                            case exifInterfaceCls.ORIENTATION_ROTATE_270:  
                                degree = 270;  
                                break;  
                        }  
                        if(orientation > 0){  
                            var matrixCls = plus.android.importClass("android.graphics.Matrix");  
                            var matrix = new matrixCls();  
                            matrix.postRotate(degree);  
                            var bitmapCls = plus.android.importClass("android.graphics.Bitmap");  
                            var bitmapFactoryCls = plus.android.importClass("android.graphics.BitmapFactory");  
                            var bitmapFactory_Options = new bitmapFactoryCls.Options();  
                            bitmapFactory_Options.inJustDecodeBounds = true;  
                            var currBitmap = bitmapFactoryCls.decodeFile(filePath, bitmapFactory_Options);  
                            var retBitmap = bitmapCls.createBitmap(currBitmap, 0, 0, currBitmap.getWidth(), currBitmap.getHeight(), matrix, true);  
                            var fileCls = plus.android.importClass("java.io.File");  
//                          plus.io.resolveLocalFileSystemURL("_doc/", function(gifEntry) {  
//                              var fileObj_path = gifEntry.fullPath + "image/";  
                                var fileObj_path = context.getFilesDir() + "/";  
                                var fileObj_name = new Date().getTime() + ".png";  
                                var fileObj = new fileCls(fileObj_path, fileObj_name);  
                                var fileOutputStreamCls = plus.android.importClass("java.io.FileOutputStream");  
                                var fileOutputStream = new fileOutputStreamCls(fileObj);  
                                retBitmap.compress(bitmapCls.CompressFormat.PNG, 100, fileOutputStream);  
                                fileOutputStream.flush();  
                                fileOutputStream.close();  
                                filePath = fileObj_path + fileObj_name;  
                                // 执行回调  
                                callback && callback({ret:1,filePath:"file://" + filePath});  
//                          });  
                        }else{  
                            // 执行回调  
                            callback && callback({ret:1,filePath:"file://" + filePath});  
                        }  
                    }  
                    // 关闭游标  
                    cursor.close();  
                    plus.nativeUI.closeWaiting();  
                }else{  
                    // 取消  
                    callback && callback({ret:-1});  
                }  
            }  
        },  
        IOS: function(callback) {  
            // var PHImageContentModeAspectFill = 1  
            // var allPhotosOptions = plus.ios.newObject("PHFetchOptions");  
            // var manager = plus.ios.importClass("PHImageManager");  
            // var defaultManager = manager.defaultManager();  
            // // plus.ios.importClass(defaultManager);  
            // var CGSizeCls = plus.ios.importClass(CGSize);  
            // var targetSize = CGSizeCls.CGSizeMake(100, 100);  
            // var PHAssetCls = plus.ios.importClass("PHAsset");  
            // var allMedias = PHAssetCls.fetchAssetsWithOptions(allPhotosOptions);  
            // plus.ios.importClass(allMedias);  
            // for (var i = 0; i < allMedias.count(); i++) {  
            //  var itor = allMedias.objectAtIndex(i);  
            //  plus.ios.importClass(itor);  
            //  if (itor.mediaType() == 1) {  
            //      // 获取图片路径未实现  
            //  }  
            //  break;  
            // }  
            plus.gallery.pick(function(p){  
                callback && callback({ret:1,filePath:p});  
            }  
            ,function(error){  
                callback && callback({ret:0,error:error});  
            },{  
                filter: "image"  
            });  
        },  
        open: function(callback) {  
            if (plus.os.name == "Android") {  
                SelectPicComm.pick.Android(callback);  
            } else {  
                SelectPicComm.pick.IOS(callback);  
            }  
        }  
    },  
    camera: {  
        Android: function(callback) {  
            var cmr = plus.camera.getCamera();  
            var res = cmr.supportedImageResolutions[0];  
            var fmt = cmr.supportedImageFormats[0];  
            console.log("Resolution: " + res + ", Format: " + fmt);  
            cmr.captureImage(function(path) {  
                callback && callback({ret:1,filePath:path});  
            },  
            function(error) {  
                callback && callback({ret:0,error:error});  
            }, {  
                resolution: res,  
                format: fmt  
            });  
        },  
        IOS: function(callback) {  
            // // 拍照闪退,并且获取不到监听结果,未实现  
            // plus.webview.create("/app/part5/camera.html", "camera", {  
            //  scrollIndicator: "none",  
            //  scalable: !1,  
            //  background: "transparent"  
            // }, {})  
        },  
        open: function(callback){  
            if (plus.os.name == "Android") {  
                SelectPicComm.camera.Android(callback);  
            } else {  
                // SelectPicComm.camera.IOS(callback);  
                SelectPicComm.camera.Android(callback);  
            }  
        }  
    },  
    open:function(callback){  
        if(mui.os.plus){  
            var a=[{  
                title:'カメラ'  
            },{  
                title:'アルバム'  
            }];  
            plus.nativeUI.actionSheet({  
                // title:'修改头像',  
                cancel:'取消',  
                buttons:a  
            },function(b){  
                switch(b.index){  
                    case 0:  
                        break;  
                    case 1:  
                        //拍照  
                        SelectPicComm.camera.open(callback);  
                        break;  
                    case 2:  
                        //打开相册  
                        SelectPicComm.pick.open(callback);  
                        break;  
                    default:  
                        break;  
                }  
            },false);  
        }  
    }  
};
你的城主大人

你的城主大人 (作者)

解决思路:
NJS代码打开的相册,选择图片后获取到uri
img加canvas根据uri获取到base64数据
使用plus.nativeObj.Bitmap对象加载图片保存到应用内目录
获取到保存后路径
如果需要转向可以考虑使用exif.js
或者转存后的exif信息没有丢失的情况下使用plus.io.getImageInfo获取orientation
获取到orientation后使用canvas进行转向即可

如果有更好的思路可以贴在下方,有被帮助到的记得点赞

该问题目前已经被锁定, 无法添加新回复