打開相冊相機截取圖片

 //上傳頭像
    private void initChange() {
        View view = View.inflate(getActivity(),R.layout.pull_image_recycleriew,null);
        camera = view.findViewById(R.id.camera);
        pick = view.findViewById(R.id.pick);
        cancel = view.findViewById(R.id.cancel);
        mPop = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
        //設置焦點
        mPop.setFocusable(true);
        //設置是否可以觸摸
        mPop.setTouchable(true);
        mPop.setBackgroundDrawable(new BitmapDrawable());
        mPop.dismiss();
        mPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                changeWindowAlfa(1f);//pop消失,透明度恢復
            }
        });
        //點擊取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPop.dismiss();
            }
        });
        mPop.setOutsideTouchable(true);
        //點擊展示
        mine_image_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //改變窗口透明度
                changeWindowAlfa(0.6f);//改變窗口透明度
                mPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);
                mPop.update();
                mPop.showAsDropDown(v, Gravity.BOTTOM,0 ,0 );
                initAddress();
            }
        });
    }

    void changeWindowAlfa(float alfa){
        WindowManager.LayoutParams params = getActivity().getWindow().getAttributes();
        params.alpha = alfa;
        getActivity().getWindow().setAttributes(params);
    }

    //設置圖片
    private void initAddress() {
        //點擊相機
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
                startActivityForResult(intent, 1000);
            }
        });
        //打開相冊
        pick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 1000);
            }
        });
    }
    //回調設置圖片
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && resultCode == RESULT_OK) {
            Uri uri = data.getData();
            Intent cIntent = new Intent("com.android.camera.action.CROP");
            cIntent.setDataAndType(uri, "image/*");
            cIntent.putExtra("crop", true);
            cIntent.putExtra("aspactX", 1);
            cIntent.putExtra("aspactY", 1);
            cIntent.putExtra("outputX", 70);
            cIntent.putExtra("outputY", 70);
            cIntent.putExtra("return-data", true);

            startActivityForResult(cIntent, 2000);
        }

        if (requestCode == 2000 && resultCode == RESULT_OK) {
            Bitmap bitmap = data.getParcelableExtra("data");

            String crop = saveImage("crop", bitmap);

            Map<String, String> map = new HashMap<>();
            Map<String, RequestBody> mapBody = new HashMap<>();
                File file=new File(crop);
                if(file.exists()){
                    RequestBody requestBody = RxpartMapUtils.toRequestBodyOfImage(file);
                    mapBody.put("image"+"\";filename=\""+file.getName(), requestBody);
                }
            iPresenter.pBodyRequest(Apis.URL_PUT_HEADPIC_POST, map, mapBody, MyPullHandPicBean.class);
        }


    }



    //保存圖片
    private String saveImage(String crop, Bitmap bitmap) {
        File appDir = new File(Environment.getExternalStorageDirectory().getPath());
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = crop + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            return file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }

工具類``

package com.zjh.administrat.torchbearer_power.bag.utilsbag;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RxpartMapUtils {
    public static RequestBody toRequestBodyOfImage(File file) {
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), file);
        return fileBody;
    }
}

//處理圖片
    @Multipart
    @POST
    rx.Observable<ResponseBody> postFormBody(@Url String urlStr,@QueryMap Map<String, String> map, @PartMap Map<String, RequestBody> requestBodyMap);


 /**
     * 表單post請求****************************
     */
    public void postFormBody(String url, Map<String, String> map, Map<String, RequestBody> mapBody, HttpListener listener) {
        if (map == null) {
            map = new HashMap<>();
        }
        if (mapBody == null){
            mapBody = new HashMap<>();
        }
        baseApi.postFormBody(url, map, mapBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(getObserver(listener));
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章