YancyYe / GalleryPick

Android 图片选择器。
454 stars 87 forks source link

问个问题 #17

Closed Jensen0618 closed 7 years ago

Jensen0618 commented 7 years ago

大神你好,我写了一个自定义的组合控件,用来上传图片用的功能很简单就是ImageView和TextView的组合,在控件里使用了你的库,选择图片然后加载到ImageView上,现在问题是这样的,页面上有多个这种控件,选择图片后,图片总是加载到最后一个控件上,不管是点击那个控件都是响应在最后的控件上,不知道是什么原因? 自定义控件代码如下:

public class ImagePicker extends RelativeLayout implements View.OnClickListener {

    private Context mContext;

    private ImageView mImageView;//这个是要用来展示上传的图片的
    private TextView mTextView;
    private ImageView mIvEx;

    private String mResIdEx;

    private String mTitleText;
    private float mTitleSize;
    private int mTitleColor;
    private Drawable mDrawableEx;

    private String mImageUrl;

    private PermissionsChecker mPermissionsChecker; // 权限检测器

    // 所需的全部权限
    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA

    };
    private static final int REQUEST_CODE = 0; // 请求码

    private BottomSheetDialog mDialog;
    private GalleryConfig mGalleryConfig;

    public String getTitleText() {
        return mTitleText;
    }

    public void setTitleText(String titleText) {
        this.mTitleText = titleText;
        mTextView.setText(mTitleText);
    }

    public String getImageUrl() {
        return mImageUrl;
    }

    public ImagePicker(Context context) {
        super(context);
        mContext = context;
    }

    public ImagePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        if (!isInEditMode()) {
            LayoutInflater.from(context).inflate(R.layout.image_picker, this, true);
        }
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImagePicker);
        mDrawableEx = typedArray.getDrawable(R.styleable.ImagePicker_imageEx);
        mResIdEx = typedArray.getString(R.styleable.ImagePicker_imageExResId);
        mTitleText = typedArray.getString(R.styleable.ImagePicker_titleText);
        mTitleColor = typedArray.getColor(R.styleable.ImagePicker_titleTextColor, Color.BLACK);
        typedArray.recycle();
    }

    public ImagePicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        if (!isInEditMode()) {
            mImageView = (ImageView) findViewById(R.id.ip_image);
            mTextView = (TextView) findViewById(R.id.ip_text);
            mIvEx = (ImageView) findViewById(R.id.ip_imageEx);
            if (!TextUtils.isEmpty(mTitleText)) {
                mTextView.setText(mTitleText);
            }
            if (mDrawableEx != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    mIvEx.setBackground(mDrawableEx);
                }
            }
            mImageView.setOnClickListener(this);
            mIvEx.setOnClickListener(this);
        }

        //图库初始化
        initGalleryPicker();
        mPermissionsChecker = new PermissionsChecker(mContext);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ip_image:
                showDialog();
                break;
            case R.id.ip_imageEx:
                lookEx();
                break;
            case R.id.tv_takePhoto:
                takePhoto();
                dismissDialog();
                break;
            case R.id.tv_Gallery:
                selectGallery();
                dismissDialog();
                break;
            case R.id.tv_cancel:
                dismissDialog();
                break;

        }
    }

    private void lookEx() {
        Resources res = getResources();

        int drawable = res.getIdentifier(mResIdEx, "drawable", mContext.getPackageName());

        mContext.startActivity(new Intent(mContext, ImageActivity.class)
                .putExtra(Constants.IMAGE, drawable));
    }

    private void takePhoto() {
        GalleryPick.getInstance().setGalleryConfig(mGalleryConfig).openCamera((Activity) mContext);
    }
    //打开相机
    private void dismissDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }
    //从图库选择
    private void selectGallery() {
        if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {
            PermissionsActivity.startActivityForResult((Activity) mContext, REQUEST_CODE, PERMISSIONS);
        }
        mGalleryConfig.getBuilder().isOpenCamera(false).build();
        GalleryPick.getInstance().setGalleryConfig(mGalleryConfig).open((Activity) mContext);
    }

    private void showDialog() {
        if (mDialog == null) {
            mDialog = new BottomSheetDialog(mContext);
            View dialogView = LayoutInflater.from(mContext).inflate(R.layout.image_picker_dialog, null);
            TextView tvTakePhoto = (TextView) dialogView.findViewById(R.id.tv_takePhoto);
            TextView tvGallery = (TextView) dialogView.findViewById(R.id.tv_Gallery);
            TextView tvCancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
            tvTakePhoto.setOnClickListener(this);
            tvGallery.setOnClickListener(this);
            tvCancel.setOnClickListener(this);
            mDialog.setContentView(dialogView);
        }
        mDialog.show();
    }

    private void initGalleryPicker() {
        mGalleryConfig = new GalleryConfig.Builder()
                .imageLoader(new GlideImageLoader())
                .iHandlerCallBack(iHandlerCallBack)
                .crop(true)
                .build();
    }

    IHandlerCallBack iHandlerCallBack = new IHandlerCallBack() {
        @Override
        public void onStart() {
        }

        @Override
        public void onSuccess(List<String> photoList) {
            if (photoList.size() > 0) {
                String path = photoList.get(0);
                //图片展示,当有多个控件时,总是加载到最后一个控件上
                Glide.with(mContext)
                        .load(path)
                        .centerCrop()
                        .into(mImageView);
                mTextView.setVisibility(View.GONE);
               //上传
                Bitmap bitmap = ImageUtils.getBitmap(path);
                byte[] imgByte = ImageUtils.bitmap2Bytes(bitmap, Bitmap.CompressFormat.JPEG);
                String imgString = Constants.BASE64_ENCODE + EncodeUtils.base64Encode2String(imgByte);
                RetrofitHelper.getInstance().upload(new RxSubscriber<String>(mContext) {
                    @Override
                    public void onNext(String s) {
                        mImageUrl = s;
                    }
                }, imgString);
            }
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onFinish() {
        }

        @Override
        public void onError() {
        }
    };

}
YancyYe commented 7 years ago

iHandlerCallBack 中的 onSuccess 返回是你选择好的图片路径, 在此你要添加个标记判断你是从哪个imageview 进入选图操作的,然后在onsuccess中判断进行显示。 我看你在onsuccess 中 Glide.with(mContext) .load(path) .centerCrop() .into(mImageView);

你这样写 不管点哪里 选择的图片都在 mImageView 中显示。