华为机型 cordova-plugin-camera从图库获取报错

简介: 华为的系统执行如下代码从图库中获取图片会报错.navigator.camera.getPicture(function(imageURI){ alert(imageURI); res...

华为的系统执行如下代码从图库中获取图片会报错.

navigator.camera.getPicture(function(imageURI){
			alert(imageURI);
			resolveLocalFileSystemURL(imageURI, function(entry) {
				alert('cdvfile URI: ' + entry.toInternalURL());
		
				 document.querySelector("#aa").src = entry.toInternalURL();//将图片在html img中显示出来
			});

			

		}, 
		function(message){

		}
		, { quality: 50,
		    destinationType: Camera.DestinationType.FILE_URI ,
		    sourceType:Camera.PictureSourceType.SAVEDPHOTOALBUM

		
		}

经过排查发现华为默认的图片浏览返回的是 "com.android.providers.media.documents"

所以要修改 cordova-plugin-camera 插件中的 CameraLauncher.java 文件

增加:

public static boolean isMediaDocument(Uri uri) {  
    return "com.android.providers.media.documents".equals(uri.getAuthority());  
}  

/**
     * Applies all needed transformation to the image received from the gallery.
     *
     * @param destType          In which form should we return the image
     * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
     */
    private void processResultFromGallery(int destType, Intent intent) {
        Uri uri = intent.getData();
        if (uri == null) {
            if (croppedUri != null) {
                uri = croppedUri;
            } else {
                this.failPicture("null data from photo library");
                return;
            }
        }
        int rotate = 0;

        // If you ask for video or all media type you will automatically get back a file URI
        // and there will be no attempt to resize any returned data
        if (this.mediaType != PICTURE) {
            this.callbackContext.success(uri.toString());
        }
        else {
            // This is a special case to just return the path as no scaling,
            // rotating, nor compressing needs to be done
            if (this.targetHeight == -1 && this.targetWidth == -1 &&
                    (destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
            	
            	if(isMediaDocument(uri)){
            		  String realPath = FileHelper.getRealPath(uri, this.cordova);
            		    this.callbackContext.success("file://" + realPath);
            	}else{
            	     this.callbackContext.success(uri.toString());
            	}
            	
           
            } else {
                String uriString = uri.toString();
                // Get the path to the image. Makes loading so much easier.
                String mimeType = FileHelper.getMimeType(uriString, this.cordova);
                // If we don't have a valid image so quit.
                if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
                    Log.d(LOG_TAG, "I either have a null image path or bitmap");
                    this.failPicture("Unable to retrieve path to picture!");
                    return;
                }
                Bitmap bitmap = null;
                try {
                    bitmap = getScaledBitmap(uriString);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (bitmap == null) {
                    Log.d(LOG_TAG, "I either have a null image path or bitmap");
                    this.failPicture("Unable to create bitmap!");
                    return;
                }

                if (this.correctOrientation) {
                    rotate = getImageOrientation(uri);
                    if (rotate != 0) {
                        Matrix matrix = new Matrix();
                        matrix.setRotate(rotate);
                        try {
                            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                            this.orientationCorrected = true;
                        } catch (OutOfMemoryError oom) {
                            this.orientationCorrected = false;
                        }
                    }
                }

                // If sending base64 image back
                if (destType == DATA_URL) {
                    this.processPicture(bitmap);
                }

                // If sending filename back
                else if (destType == FILE_URI || destType == NATIVE_URI) {
                    // Did we modify the image?
                    if ( (this.targetHeight > 0 && this.targetWidth > 0) ||
                            (this.correctOrientation && this.orientationCorrected) ) {
                        try {
                            String modifiedPath = this.ouputModifiedBitmap(bitmap, uri);
                            // The modified image is cached by the app in order to get around this and not have to delete you
                            // application cache I'm adding the current system time to the end of the file url.
                            this.callbackContext.success("file://" + modifiedPath + "?" + System.currentTimeMillis());
                        } catch (Exception e) {
                            e.printStackTrace();
                            this.failPicture("Error retrieving image.");
                        }
                    }
                    else {
                        this.callbackContext.success(uri.toString());
                    }
                }
                if (bitmap != null) {
                    bitmap.recycle();
                    bitmap = null;
                }
                System.gc();
            }
        }
    }



目录
相关文章
|
机器学习/深度学习 算法 计算机视觉
如何利用Python实现图像识别中的目标检测
Python是一种功能强大的编程语言,可以用于图像识别中的目标检测。本文将介绍如何使用Python和OpenCV库实现目标检测的基本原理和方法,并提供实用的代码示例。
|
12月前
|
缓存 前端开发 JavaScript
Vue3 写法示例与规范指南
Vue3项目规范指南:统一目录结构、命名规则与Composition API用法,涵盖组件通信、性能优化及ESLint+Prettier工具链配置,提升代码可维护性与团队协作效率。(239字)
1458 1
|
8月前
|
SQL 分布式计算 关系型数据库
Dataphin功能Tips系列(92)如何方便快速地通过SQL取数实现定制化数据同步
本文介绍如何在Dataphin中实现MySQL到MaxCompute的T-1增量数据同步:通过离线集成任务,将MySQL输入组件切换为脚本模式,利用`querySql`配合`${extract_date}`动态参数(默认`${yyyy-MM-dd}`),精准抽取前一日订单数据,配置调度后即可全自动运行。
441 5
|
5月前
|
数据采集 人工智能 机器人
从手动搜货到 AI 全自动选品|OpenClaw item_search + 多 Agent 落地实战,小白也能搭建无人选品机器人
本文介绍基于OpenClaw淘宝`item_search`接口与AI多智能体协同的自动化选品方案:一句话指令即可完成关键词拓展、全页抓取、智能筛选、详情深挖及Excel铺货报表生成,全程无人值守。附可直接运行的Python源码,适配个人卖家、工作室及ERP开发,大幅提升选品效率与准确性。(239字)
|
9月前
|
人工智能 弹性计算 安全
阿里云服务器经济型e、通用算力型u1/u2i/u2a、九代c9i/g9i/r9i/c9a/g9a/r9a实例区别与选择参考
阿里云活动中的云服务器ECS提供多种实例规格,包括经济型e实例、通用算力型u1/u2i/u2a及九代c9i/g9i/r9i/c9a/g9a/r9a实例。经济型e实例适合个人开发者、学生及小微企业,价格亲民;通用算力型实例性能均衡,满足多元业务需求;九代x86/ARM实例则以高性能、安全加密及高效能特性,满足计算密集、内存密集及成本敏感场景。用户可根据需求及预算选择合适的实例规格。
|
10月前
|
人工智能 自然语言处理 搜索推荐
2026AI数字人全栈技术类企业
AI数字人全栈技术涵盖感知、认知、生成、渲染与交互全流程,融合ASR、NLU、LLM、TTS、3D动画与多模态交互,应用于虚拟客服、教育、医疗等领域。依托Unity、Unreal、WebGL等引擎,结合大模型与轻量化部署,推动数字人在元宇宙、智能终端中广泛应用,实现从技术演示到产业落地的跨越。
|
11月前
|
关系型数据库 应用服务中间件 nginx
容器引擎Docker
Docker是一种容器化技术,通过镜像打包应用及依赖,实现跨环境无缝迁移。它利用容器隔离运行,解决开发、测试、生产环境不一致问题,相比虚拟机更轻量、高效,提升部署与运维效率。
容器引擎Docker
|
存储 移动开发 安全
支付宝H5下载被拦截的原因排查与解决指南
在支付宝H5页面中,“请确保该下载文件来源安全”的提示通常源于支付宝的安全拦截机制。原因可能包括:1) 安全策略限制,如非HTTPS协议或未通过白名单审核;2) 文件下载方式不规范,如自动触发下载;3) CSP配置不当。解决方案包括:确保使用HTTPS协议、让用户主动触发下载、正确配置CSP、检查文件类型与存储位置、申请支付宝白名单以及遵循官方文档规范。调试时可查看控制台日志和网络请求状态,必要时联系支付宝技术支持协助排查。
1412 0
|
前端开发 JavaScript API
|
存储 缓存 NoSQL
这些年背过的面试题——Redis篇
本文是技术人面试系列Redis篇,面试中关于Redis都需要了解哪些基础?一文带你详细了解,欢迎收藏!
1305 17