Unable to load image when selected from the gallery on Android 4.4 (KitKat) using PhoneGap Camera Plugin

后端 未结 6 437
小蘑菇
小蘑菇 2020-12-01 06:15

I\'m trying to set the source of an img tag in my app based on the image chosen from the device image gallery using the PhoneGap/Cordova Camera Plugin.

It has worked

6条回答
  •  盖世英雄少女心
    2020-12-01 07:03

    Not much more robust just a few more checks for special conditions like "content:" with no extensions and so. Also, since I need to upload it, I am detecting the extension, or create a .jpg extension if the file does not have one:

    // Android 4.4 cordova workarounds ... returns new kind or URL for content from chooser
                    //if (imageUrl.substring(0,21)=="content://com.android") {
                    if(imageUrl.indexOf('content://') != -1 && imageUrl.indexOf("%3A") != -1){
                        //"PlainFileUrl = content://com.android.providers.media.documents/document/image%3A14",
                        photo_split=imageUrl.split("%3A");
                        imageUrl="content://media/external/images/media/"+photo_split[1];
                    }
                    // workaround end
    
                    var fileName = imageUrl.substr(imageUrl.lastIndexOf('/') + 1);
                    var extension;
    
                    // check for content: protocol to make sure is not
                    // a file with no extension
                    if (imageUrl.indexOf('content://') != -1) {
                        if(imageUrl.lastIndexOf('.') > imageUrl.lastIndexOf('/')){
                            extension = imageUrl.substr(imageUrl.lastIndexOf('.') + 1);
                        }else{
                            extension = "jpg";
                            fileName = fileName + ".jpg";
                            LogService.log("Created File Extension jpg");
                        }
                    } else {
                        if (imageUrl.lastIndexOf('.') == -1 || (imageUrl.lastIndexOf('.') < imageUrl.lastIndexOf('/')) ) {
                            extension = "invalid";
                        } else {
                            extension = imageUrl.substr(imageUrl.lastIndexOf('.') + 1);
                        }
                    }
    

提交回复
热议问题