图库相机更换接口图片

compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.google.code.gson:gson:2.2.4'

MainActivity的代码

private ImageView iv;
File f=new File(Environment.getExternalStorageDirectory(),"ni.jpg");
private String set="https://www.zhaoapi.cn/file/upload?uid=10134";
Getjson getjson=new Getjson();
private String icon;

private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        if (msg.what==1) {
            ImageLoader.getInstance().displayImage(icon,iv);
            //设置图片后从新调用加载图片的方法
           gettu();
        }
    }
};
private String net="https://www.zhaoapi.cn/user/getUserInfo?uid=10134";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iv = findViewById(R.id.iv);
    iv.setImageResource(R.drawable.q);
       gettu();
    //图片的点击事件
    btiv();

}



private void gettu() {
    //给图片设置数据
    getjson.okgetnet(net);
    getjson.setOnOKHttpGetListener(new Getjson.OkhttpgetListener() {

        @Override
        public void error(String error) {

        }

        @Override
        public void success(String json) {
            Log.e("我的线程",Thread.currentThread().getName());
            Gson gson = new Gson();
            Mybean mybean = gson.fromJson(json, Mybean.class);
            icon = mybean.getData().getIcon();
            //因为imagload运行在子线程
            //通过handler发送到主线程  在主线程中进行imageload加载图片
            handler.sendEmptyMessage(1);

        }
    });
}

private void btiv() {
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder ab=new AlertDialog.Builder(MainActivity.this);
            String[] shu={"从图库选择一张图片","从新拍一张"};
            ab.setItems(shu, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                   switch (which) {
                         case 0:
                             onStop();
                                Intent intent=new Intent(Intent.ACTION_PICK);
                                intent.setType("image/*");
                                startActivityForResult(intent, 1);

                            break;
                        case 1:
                            onStop();
                            Intent intent1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                            startActivityForResult(intent1, 2);

                             break;
                }
                }
            });
          ab.show();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //图库照片的路径
    if (requestCode==1&&resultCode==RESULT_OK) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = this.getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        File f=new File(picturePath);
        Myutil.uploadPic(set,f,"a.jpg");
        Log.d("相册路径", "onActivityResult: "+picturePath);


    }

    //相机的路径为f File f=new File(Environment.getExternalStorageDirectory(),"ni.jpg");
    if (requestCode==2&&resultCode==RESULT_OK) {
        Myutil.uploadPic(set,f,"a.jpg");


    }
    super.onActivityResult(requestCode, resultCode, data);
}
//工具类的代码  
//第一个参数为url路径  第二个为图片的路径  第三个为图片的名字
 public static void uploadPic(String url, File file, String fileName) {
    //创建OkHttpClient请求对象

    OkHttpClient okHttpClient = new OkHttpClient();
    //创建RequestBody 封装file参数
    RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
    //创建RequestBody 设置类型等
    RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build();
    //创建Request
    Request request = new Request.Builder().url(url).post(requestBody).build();

    //得到Call
    Call call = okHttpClient.newCall(request);
    //执行请求
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("失败", "onResponse: 很不高兴");
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            //上传成功回调 目前不需要处理
            Log.d("成功", "onResponse: 开心");
        }
    });

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值