Android multi upload pictures to the server php (php include multi-map interface to upload detailed implementation)

Due

Due to business needs, company personnel files of information needs to be rationalized management involves a resume and picture ID card pictures uploaded to the staff of the php server (due to uncertainty when employees upload pictures omissions, select multi-map upload )
PS: the database has given me, I am responsible for implementation and end Android interface to upload pictures to the interface and the interface will be added to the data in the database

The effect of the database field to be achieved:
that is how much upload pictures in the designated field (such as my img field) will be divided with a few commas
Here Insert Picture Description

through

Single image upload php and interfaces

Ado Bibi started directly :( I wherein the image end Android Reference Selection) -> here enter detailed tutorial image imitation micro channel selector effect
achieved renderings of selected image effect will not refer to the link below in detail
renderings :
Here Insert Picture Description
specific code implementation:
Android side:


private static final int REQUEST_SELECT_IMAGES_CODE = 0x01;
private ArrayList<String> mImagePathf;
//点击图片上传button点击事件的业务处理
binding.starffRecordZssc.setOnClickListener(new View.OnClickListener() {//证书上传
            @Override
            public void onClick(View v) {
                ImagePicker.getInstance()
                        .setTitle("标题")//设置标题
                        .showCamera(true)//设置是否显示拍照按钮
                        .showImage(true)//设置是否展示图片
                        .showVideo(true)//设置是否展示视频
                        .setMaxCount(1)//设置最大选择图片数目(默认为1,单选)
                        .setSingleType(true)//设置图片视频不能同时选择
                        .setImagePaths(mImagePathf)//设置历史选择记录
                        .setImageLoader(new GlideLoader())//设置自定义图片加载器
                        .start(StarffRecordAdd.this, REQUEST_SELECT_IMAGES_CODE);//REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode
            }
        });

//选择图片后的回调
  @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_IMAGES_CODE && resultCode == RESULT_OK) {
            mImagePathf =data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
            Bitmap bm = BitmapFactory.decodeFile(mImagePathf.get(0));//设置加载第一个图片
            String img = ChangeBase.bitmapToBase64(bm);//将图片转换为Base64的格式 准备往接口中发送
        } 
    }

Convert the picture to the Base64 class:

public class ChangeBase {
    public static  String bitmapToBase64(Bitmap bitmap) {
        // 要返回的字符串
        String reslut = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                /**
                 * 压缩只对保存有效果bitmap还是原来的大小
                 */
                bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);
                try {
                    baos.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                baos.close();
                // 转换为字节数组
                byte[] byteArray = baos.toByteArray();
                // 转换为字符串
                reslut = Base64.encodeToString(byteArray, Base64.DEFAULT);
                Log.i("sss",reslut);
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return reslut;
    }
    }

ok, so android client even get
and then we take a look at to achieve a single view of upload php interface end with

   public function test(){
    $model = D('Atest');
    $data = array();
    $jsonarr = array();
    $data['img'] = I('post.img');
    $name=time();
    if(!empty($data['img'])){
      //目录
      $rootPath = './Public/data/hr/xuelizhengshu/';
      $path = 'data/hr/xuelizhengshu/';//文件上传子目录
      $data['img']=base64_decode($data['img']);
      file_put_contents($rootPath.$name.'.jpg',$data['img']);
      $data['img']=$path.$name.'.jpg';
    }
     $datalist=$model->add($data);
     if($datalist){
        $jsonarr['state'] = "access";
        $jsonarr['date'] = $datalist;
       }
  header('Content-Type:text/html; charset=utf-8');
  exit(json_encode($jsonarr,JSON_UNESCAPED_UNICODE));
}

ok then only need to img sent to the interface in the android client interfaces will then writes the data to our database like this
Here Insert Picture Description

And multi-image upload php Interface

Here Insert Picture Description
Multi single image upload pictures and upload ideas almost
android client implementation:

 private static final int REQUEST_SELECT_IMAGES_SECOND = 0x02;
 private ArrayList<String> mImagePaths;
 private Map<String,String> jianli;
 private int shenfencount = 0;
 //多图片上传button的点击事件处理
 binding.starffRecordSfzfyj.setOnClickListener(new View.OnClickListener() {//身份证复印件
            @Override
            public void onClick(View v) {
                ImagePicker.getInstance()
                        .setTitle("标题")//设置标题
                        .showCamera(true)//设置是否显示拍照按钮
                        .showImage(true)//设置是否展示图片
                        .showVideo(true)//设置是否展示视频
                        .setMaxCount(5)//设置最大选择图片数目(默认为1,单选)
                        .setSingleType(true)//设置图片视频不能同时选择
                        .setImagePaths(mImagePaths)//设置历史选择记录
                        .setImageLoader(new GlideLoader())//设置自定义图片加载器
                        .start(StarffRecordAdd.this, REQUEST_SELECT_IMAGES_SECOND);//REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode
            }
        });

//回调处理
  @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
       if (requestCode == REQUEST_SELECT_IMAGES_SECOND && resultCode == RESULT_OK) {
            mImagePaths = data.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES);
               jianli = new HashMap<>();
               jianli.clear();
               for(int i=0;i<mImagePaths.size();i++){
    jianli.put("jianliimg"+i,ChangeBase.bitmapToBase64(BitmapFactory.decodeFile(mImagePaths.get(i)))); 
               }
            shenfencount= mImagePaths.size();
        }

    }

Multi-image upload difficulty is unable to determine the number of images you want to upload, and send the last number of arguments
which I use Retrofit network requests and then I use the built-in variable parameters @FieldMap
network parameters of the request

    //这里我使用的是kotlin的写的
    @FormUrlEncoded
    @POST("Hrmdanganjson/add")//人员档案添加数据
    fun getrecordaddadd(@Field("shenfencount") shenfencount:Int, @FieldMap jianli:Map<String,String>

ok, so we only need to number our image and get into Base64 all the pictures sent to the interface to this end we Andrews is the end of the operation

php multi picture upload interface:

  public function test(){
    $model = D('Atest');
    $data = array();
    $jsonarr = array();
    $shefenzhengcount = I('post.shenfencount');
    $name=time();
     for ($x=0; $x<$shefenzhengcount; $x++) {//拿到身份证上传图片的个数  ->3
        $current = I('post.jianliimg'.$x);
        $name+=2;
        $rootPath = './Public/data/hr/jianlishangchuan/';
        $path = 'data/hr/jianlishangchuan/';
        $current=base64_decode($current);
        file_put_contents($rootPath.$name.'.jpg',$current);
        $shuzu[]=$path.$name.'.jpg';
     }
     $data['jianliimg']=implode(',',$shuzu);
     $datalist=$model->add($data);
     if($datalist){
        $jsonarr['state'] = "access";
        $jsonarr['date'] = $datalist;
       }
  header('Content-Type:text/html; charset=utf-8');
  exit(json_encode($jsonarr,JSON_UNESCAPED_UNICODE));
}

We loop for temporary storage of the image field corresponding to the key field is the end of the HashMap android in php depending on the number of images
and then in each iteration loop for good data into an array for the outer loop then our database holds more map that field to receive this array do not forget to be separated by commas yo

ok achieve this end imitation micro-channel selector picture effects, please refer to my https://blog.csdn.net/yuhang01/article/details/102398127 remember
there are detailed usage

result

Each effect will be to achieve a programmer to kill an old iron were difficult for me

Published 43 original articles · won praise 60 · views 6740

Guess you like

Origin blog.csdn.net/yuhang01/article/details/103681520