华为机型 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();
            }
        }
    }



目录
相关文章
|
3月前
|
程序员 开发者
纯血鸿蒙来画龙!基于HarmonyOS ArkTS来操作SVG图片
大家好,龙年报喜,大地回春,作为程序员,以代码之名,表达对于龙年的祝福。本节将演示如何在基于HarmonyOS ArkTS的Image组件来实现画一条中国龙,祝大家“码”上“鸿”福到!
117 0
|
2月前
|
存储 数据库 Android开发
安卓Jetpack Compose+Kotlin,支持从本地添加音频文件到播放列表,支持删除,使用ExoPlayer播放音乐
为了在UI界面添加用于添加和删除本地音乐文件的按钮,以及相关的播放功能,你需要实现以下几个步骤: 1. **集成用户选择本地音乐**:允许用户从设备中选择音乐文件。 2. **创建UI按钮**:在界面中创建添加和删除按钮。 3. **数据库功能**:使用Room数据库来存储音频文件信息。 4. **更新ViewModel**:处理添加、删除和播放音频文件的逻辑。 5. **UI实现**:在UI层支持添加、删除音乐以及播放功能。
131 1
|
2月前
|
监控 Android开发 数据安全/隐私保护
安卓kotlin JetPack Compose 实现摄像头监控画面变化并录制视频
在这个示例中,开发者正在使用Kotlin和Jetpack Compose构建一个Android应用程序,该程序 能够通过手机后置主摄像头录制视频、检测画面差异、实时预览并将视频上传至FTP服务器的Android应用
140 1
|
2月前
|
缓存 Android开发 Kotlin
【安卓app开发】kotlin Jetpack Compose框架 | 先用OKhttp下载远程音频文件再使用ExoPlayer播放
使用 Kotlin 的 Jetpack Compose 开发安卓应用时,可以结合 OkHttp 下载远程音频文件和 ExoPlayer 进行播放。在 `build.gradle` 添加相关依赖后,示例代码展示了如何下载音频并用 ExoPlayer 播放。代码包括添加依赖、下载文件、播放文件及简单的 Compose UI。注意,示例未包含完整错误处理和资源释放,实际应用需补充这些内容。
129 5
|
10月前
|
JavaScript Java 开发工具
Cocos Creator Android 平台接入 Google Firebase (Analytics功能)(二)
Cocos Creator Android 平台接入 Google Firebase (Analytics功能)
273 0
|
3月前
|
数据采集 JavaScript 前端开发
toolkit-frame之toolkit-sprider(数据采集)---百度图片
toolkit-frame之toolkit-sprider(数据采集)---百度图片
26 2
|
10月前
|
Android开发 开发者
Cocos Creator Android 平台接入 Google Firebase (Analytics功能)(一)
Cocos Creator Android 平台接入 Google Firebase (Analytics功能)
241 1
|
Android开发
OPPO手机调试Android Flutter APP时每次都要提示重新安装且不能hot reload
OPPO手机调试Android Flutter APP时每次都要提示重新安装且不能hot reload
|
XML 数据格式
【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现
【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现
116 1
【HarmonyOS】【DevEco Studio】NOTE05:PageAbility生命周期的呈现
|
开发工具 图形学
Pico SDK在Unity中如何控制移动
Unity导入Pico SDK后如何进行实际开发呢?想必大家都很好奇。从去年的省赛到今年的国赛, 本人研究Pico有快一年了,刚开始学习的时候自己翻过很多的博主文章,都没有找到自己想要的知识,从那时起,我便下定决心,等国赛过后 自己要写写虚拟设备的使用了,那就以我的视角来为大家讲解Pico SDK的正确使用。
688 1
Pico SDK在Unity中如何控制移动