上传图片实现

来源:互联网 发布:2016网络彩票重启时间 编辑:程序博客网 时间:2024/05/29 15:27

      我们往后台传递数据时往往少不了图片,那么就要解决这个问题,我先给一个工具类

package com.example.heatworld.maintian_merchantedition.utils;import android.content.Context;import android.util.Log;import android.widget.Toast;import com.example.heatworld.maintian_merchantedition.bean.UpLoadImageBean;import com.google.gson.Gson;import org.xutils.common.Callback;import org.xutils.http.RequestParams;import org.xutils.x;import java.io.File;import static com.example.heatworld.maintian_merchantedition.application.MyApplication.key;public class UpLoadFileTool {   public static void uploadImage(String url, String filePath, final Context context,final InterfaceCallBack interfaceCallBack){       RequestParams params = new RequestParams(url);       params.setMultipart(true);       params.addBodyParameter("key", key,"multipart/form-data");       params.addBodyParameter("file",new File(filePath),"multipart/form-data");       x.http().post(params, new Callback.CacheCallback<String>() {           @Override           public void onSuccess(String result) {               Log.i("TAG", "上传图片onSuccess: "+result);               Gson gson=new Gson();               UpLoadImageBean upLoadImageBean= gson.fromJson(result, UpLoadImageBean.class);               if(upLoadImageBean.getCode().equals("1")) {                   interfaceCallBack.callBack(upLoadImageBean.getSave_path());                   Toast.makeText(context, "上传成功", Toast.LENGTH_SHORT).show();               }else {                   Toast.makeText(context, upLoadImageBean.getMsg()+"", Toast.LENGTH_SHORT).show();               }           }           @Override           public void onError(Throwable ex, boolean isOnCallback) {               Log.i("TAG", "上传图片onError: "+ex);               Toast.makeText(context, "上传失败,请检查网络", Toast.LENGTH_SHORT).show();           }           @Override           public void onCancelled(CancelledException cex) {           }           @Override           public void onFinished() {           }           @Override           public boolean onCache(String result) {               return false;           }       });   }}

然后我们调用的时候这么写:

UpLoadFileTool.uploadImage(ip, cutFile.getPath(), context, new CallBack()...)

      三个参数,第一个是地址,第二个是图片路径,第三个是对象,第四个是一个回调,用来返回图片的,我们看见了一个图片路径,所以下面来解决图片的获取问题,图片可以通过照相和相册获取,那么我们需要在当前的Activity里面配置两个方法。

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        switch (requestCode) {            case PHOTO_REQUEST_GALLERY://获取相册传来的图片                if (data != null) {                    Uri uri = data.getData();//裁剪                    CutPhotoTool.cutPhoto(uri, VenueAuditActivity.this);                }                break;            case PHOTO_REQUEST_CAREMA://获取相机传来的东西                final Uri uri = Uri.fromFile(output);                CutPhotoTool.cutPhoto(uri, VenueAuditActivity.this);                break;            case PHOTO_REQUEST_CUT://获取裁剪之后的图片                if (data != null) {                    Bitmap bitmap = data.getParcelableExtra("data");                        case 0://法人身份证照片                            getPhotosIdCard.setImageBitmap(bitmap);                            break;                    }                    File cutFile = CutPhotoTool.getCutFile(bitmap);                    UpLoadFileTool.uploadImage(ip + Post_Photo, cutFile.getPath(), VenueAuditActivity.this, new InterfaceCallBack() {                        @Override                        public void callBack(String... i) {                                    idCardUri = i[0];//身份证照                                    break;                        }                    });//上传图片                }                break;        }        super.onActivityResult(requestCode, resultCode, data);    }    //动态权限获取返回    @Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {        switch (requestCode) {            case REQUEST_CODE_ASK_PHOTOS://相册权限以获取                CallPhotoOrCameraTool.callPhoto(this);                break;            case REQUEST_CODE_ASK_CAMERA://相机权限以获取                output = CutPhotoTool.getOutFile();                CallPhotoOrCameraTool.callCamera(this, output);                break;            default:                super.onRequestPermissionsResult(requestCode, permissions, grantResults);        }    }

      这里我们看见了当我们获取到了图片后我们就会马上上传。然后还有一个idCardUri 参数,是ImageView,返回给这个类,以便做其他的处理。
这样的其实我们就已经完成图片的上传了,下面给出demo,哈哈,其实我也没讲好,只能给例子了。
别点我,我怕疼!

原创粉丝点击