第三方解決頭像imagePicker

一、添加依賴bash

implementation 'com.lzy.widget:imagepicker:0.6.1'
複製代碼

能夠經過gradle或者Jar包依賴,但對minSdkVersion 19,不能小於19.ide

二、配置Imageloadergradle

public class pickerImageLoader implements ImageLoader {
    @Override
    public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
        Glide.with(activity).load(Uri.fromFile(new File(path))).diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
    }

    @Override
    public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {

    }

    @Override
    public void clearMemoryCache() {

    }
}複製代碼

三、在Appliacation裏onCreate()添加,而後在Manifest裏添加到name。spa

@Override
public void onCreate() {
    super.onCreate();
    pickerUtils();
    ImagePicker imagePicker = ImagePicker.getInstance();
    imagePicker.setImageLoader(new pickerImageLoader());   //設置圖片加載器
    imagePicker.setMultiMode(false);
    imagePicker.setShowCamera(true);  //顯示拍照按鈕
    imagePicker.setCrop(true);        //容許裁剪(單選才有效)
    imagePicker.setSaveRectangle(true); //是否按矩形區域保存
    imagePicker.setSelectLimit(9);    //選中數量限制
    imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形狀
    imagePicker.setFocusWidth(800);   //裁剪框的寬度。單位像素(圓形自動取寬高最小值)
    imagePicker.setFocusHeight(800);  //裁剪框的高度。單位像素(圓形自動取寬高最小值)
    imagePicker.setOutPutX(1000);//保存文件的寬度。單位像素
    imagePicker.setOutPutY(1000);//保存文件的高度。單位像素
}複製代碼

四、在頭像設置的點擊事件裏面添加如下代碼code

Intent intent = new Intent(getActivity(), ImageGridActivity.class);
startActivityForResult(intent, IMAGE_PICKER);複製代碼

五、而後在intent的回調中事件

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
        if (data != null && requestCode == IMAGE_PICKER) {
            ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            SpUtils.getInstance(getActivity()).saveMsg(getActivity(),"myImc",images.get(0).path);
            showHead();
            Log.i("imgsss", "images=" + images.size() + " images.get(0).path" + images.get(0).path);
        }
    }
}複製代碼
相關文章
相關標籤/搜索