Search in sources :

Example 1 with LocalMedia

use of com.luck.picture.lib.entity.LocalMedia in project KL2 by jweihao.

the class SendWeiBoActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch(requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                mSelectLists = PictureSelector.obtainMultipleResult(data);
                // 如果裁剪并压缩了,已取压缩路径为准,因为是先裁剪后压缩的
                for (LocalMedia media : mSelectLists) {
                    Log.i("图片-----》", media.getPath());
                }
                mGridImageAdapter.setList(mSelectLists);
                mGridImageAdapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }
}
Also used : LocalMedia(com.luck.picture.lib.entity.LocalMedia)

Example 2 with LocalMedia

use of com.luck.picture.lib.entity.LocalMedia in project KL2 by jweihao.

the class GridImageAdapter method onBindViewHolder.

/**
 * 设置值
 */
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    // 少于8张,显示继续添加的图标
    if (getItemViewType(position) == TYPE_CAMERA) {
        viewHolder.mImg.setImageResource(R.drawable.addimg_1x);
        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mOnAddPicClickListener.onAddPicClick();
            }
        });
        viewHolder.ll_del.setVisibility(View.INVISIBLE);
    } else {
        viewHolder.ll_del.setVisibility(View.VISIBLE);
        viewHolder.ll_del.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                int index = viewHolder.getAdapterPosition();
                // 通过源码分析应该是bindViewHolder()暂未绘制完成导致,知道原因的也可联系我~感谢
                if (index != RecyclerView.NO_POSITION) {
                    list.remove(index);
                    notifyItemRemoved(index);
                    notifyItemRangeChanged(index, list.size());
                }
            }
        });
        LocalMedia media = list.get(position);
        int mimeType = media.getMimeType();
        String path = "";
        if (media.isCut() && !media.isCompressed()) {
            // 裁剪过
            path = media.getCutPath();
        } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
            // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
            path = media.getCompressPath();
        } else {
            // 原图
            path = media.getPath();
        }
        // 图片
        if (media.isCompressed()) {
            Log.i("compress image result:", new File(media.getCompressPath()).length() / 1024 + "k");
            Log.i("压缩地址::", media.getCompressPath());
        }
        Log.i("原图地址::", media.getPath());
        int pictureType = PictureMimeType.isPictureType(media.getPictureType());
        if (media.isCut()) {
            Log.i("裁剪地址::", media.getCutPath());
        }
        long duration = media.getDuration();
        viewHolder.tv_duration.setVisibility(pictureType == PictureConfig.TYPE_VIDEO ? View.VISIBLE : View.GONE);
        if (mimeType == PictureMimeType.ofAudio()) {
            viewHolder.tv_duration.setVisibility(View.VISIBLE);
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.picture_audio);
            StringUtils.modifyTextViewDrawable(viewHolder.tv_duration, drawable, 0);
        } else {
            Drawable drawable = ContextCompat.getDrawable(context, R.drawable.video_icon);
            StringUtils.modifyTextViewDrawable(viewHolder.tv_duration, drawable, 0);
        }
        viewHolder.tv_duration.setText(DateUtils.timeParse(duration));
        if (mimeType == PictureMimeType.ofAudio()) {
            viewHolder.mImg.setImageResource(R.drawable.audio_placeholder);
        } else {
            RequestOptions options = new RequestOptions().centerCrop().placeholder(R.color.color_f6).diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(viewHolder.itemView.getContext()).load(path).apply(options).into(viewHolder.mImg);
        }
        // itemView 的点击事件
        if (mItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int adapterPosition = viewHolder.getAdapterPosition();
                    mItemClickListener.onItemClick(adapterPosition, v);
                }
            });
        }
    }
}
Also used : RequestOptions(com.bumptech.glide.request.RequestOptions) Drawable(android.graphics.drawable.Drawable) LocalMedia(com.luck.picture.lib.entity.LocalMedia) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View) File(java.io.File)

Example 3 with LocalMedia

use of com.luck.picture.lib.entity.LocalMedia in project KL2 by jweihao.

the class SendWeiBoActivity method initData.

private void initData() {
    FullyGridLayoutManager fullyGridLayoutManager = new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL, false);
    mRcShowPicture.setLayoutManager(fullyGridLayoutManager);
    mGridImageAdapter = new GridImageAdapter(this, mOnAddPicClickListener);
    mGridImageAdapter.setList(mSelectLists);
    mGridImageAdapter.setSelectMax(mMaxSelectNum);
    mRcShowPicture.setAdapter(mGridImageAdapter);
    mGridImageAdapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {

        @Override
        public void onItemClick(int position, View v) {
            if (mSelectLists.size() > 0) {
                LocalMedia media = mSelectLists.get(position);
                String pictureType = media.getPictureType();
                int mediaType = PictureMimeType.pictureToVideo(pictureType);
                switch(mediaType) {
                    case 1:
                        // 预览图片,可自定长按保存路径
                        PictureSelector.create(SendWeiBoActivity.this).externalPicturePreview(position, mSelectLists);
                        break;
                    case 2:
                        // 预览视频
                        PictureSelector.create(SendWeiBoActivity.this).externalPictureVideo(media.getPath());
                        break;
                    case 3:
                        // 预览音频
                        PictureSelector.create(SendWeiBoActivity.this).externalPictureAudio(media.getPath());
                    default:
                        break;
                }
            }
        }
    });
    // 清空图片缓存,包括裁剪、压缩后的图片 注意:必须要在上传完成后调用 必须要获取权限
    RxPermissions permissions = new RxPermissions(this);
    permissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Observer<Boolean>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Boolean aBoolean) {
            if (aBoolean) {
                PictureFileUtils.deleteCacheDirFile(SendWeiBoActivity.this);
            } else {
                Toast.makeText(SendWeiBoActivity.this, getString(R.string.picture_jurisdiction), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) RxPermissions(com.luck.picture.lib.permissions.RxPermissions) GridImageAdapter(com.jweihao.jdemo.adapter.GridImageAdapter) FullyGridLayoutManager(com.jweihao.jdemo.adapter.FullyGridLayoutManager) BindView(butterknife.BindView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) LocalMedia(com.luck.picture.lib.entity.LocalMedia)

Aggregations

LocalMedia (com.luck.picture.lib.entity.LocalMedia)3 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 Drawable (android.graphics.drawable.Drawable)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 RequestOptions (com.bumptech.glide.request.RequestOptions)1 FullyGridLayoutManager (com.jweihao.jdemo.adapter.FullyGridLayoutManager)1 GridImageAdapter (com.jweihao.jdemo.adapter.GridImageAdapter)1 RxPermissions (com.luck.picture.lib.permissions.RxPermissions)1 Disposable (io.reactivex.disposables.Disposable)1 File (java.io.File)1