phonegap(cordova) 入門 7----phonegap 多圖片上傳

phonegap 多圖片上傳

官方插件應該是隻有單選,而且調用的是默認本地相冊,毫無美感,作爲常用功能之一,那這也算是一個利器了,用了提神醒腦,哈哈

android 和ios 分別引用不同的原生插件,感謝開源,感謝github,分享以表支持
android 中使用了
https://github.com/AizazAZ/Android-Ultra-Photo-Selector
需要先自定義自己的phonegap插件,然後再自己的插件中調用此插件,部分代碼如下


//call
            Intent intent = new Intent(this.cordova.getActivity()
                    .getBaseContext(), PhotoSelectorActivity.class);
            intent.putExtra(PhotoSelectorActivity.KEY_MAX, 8);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

            this.cordova.startActivityForResult((CordovaPlugin) this, intent,
                    SELECT_IMAGE_CODE);


            PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
            r.setKeepCallback(true);
            callbackContext.sendPluginResult(r);
            return true;

//callback

        if (requestCode == SELECT_IMAGE_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                @SuppressWarnings("unchecked")
                List<PhotoModel> photos = (List<PhotoModel>) intent.getExtras()
                        .getSerializable("photos");
                if (photos == null || photos.isEmpty()) {

                } else {

                    String files = "{\"files\":[";
                    for(int i=0;i<photos.size();i++){
                        if(i==0){
                            String file = "{\"path\":\""+photos.get(i).getOriginalPath()+"\"}";

                            files+=file;
                        }else{
                            String file = ",{\"path\":\""+photos.get(i).getOriginalPath()+"\"}";

                            files+=file;
                        }
                    }
                    files+="]}";




                    this.callbackContext.success(files);
                }

            }
        }







IOS 中使用了
https://github.com/MakeZL/MLSelectPhoto
需要先自定義自己的phonegap插件,然後再自己的插件中調用此插件,代碼如下

 //call   //callback  
 MLSelectPhotoPickerViewController *pickerVc = [[MLSelectPhotoPickerViewController alloc] init];
pickerVc.status = PickerViewShowStatusCameraRoll;

pickerVc.maxCount = 9;


[pickerVc showPickerVc: self.window.rootViewController];
__weak typeof(self) weakSelf = self;

selectImagesCallbacked = false;
pickerVc.callBack = ^(NSArray *assets){

    //only once
    if(!selectImagesCallbacked)
    {

        selectImagesCallbacked = true;
        int i;
        NSString *files =@"{\"files\":[";



        for (i=0; i<[assets count]; i++) {
            MLSelectPhotoAssets *item = [assets objectAtIndex:i];
            NSString *file =[NSString stringWithFormat:@"{\"path\":\"%@\"}",[[item assetURL] absoluteString]];
            if(i==0){
                files = [NSString stringWithFormat:@"%@%@",files,file];
            }else{
                files = [NSString stringWithFormat:@"%@%@%@",files,@",",file];
            }
        }
        files = [NSString stringWithFormat:@"%@%@",files,@"]}"];

        NSLog(@"selected >>>>%@",files);

        NSString *js =  [[NSString alloc]initWithFormat:@"imgUpload.iosMutiImagesSelected('%@')",  files  ];
        [self runJS:js];
    }
};

“`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章