Android实现图片/图片/视频选择、预览、编辑与拍照

采用开源框架 phoenix

1使用步骤

添加依懒

//图片/视频选择、预览、编辑与拍照
implementation 'com.github.guoxiaoxing:phoenix:1.0.15'
//选填 - 图片压缩,开启功能:Phoenix.with().enableCompress(true),获取结果:MediaEntity.getCompressPath()
implementation 'com.github.guoxiaoxing:phoenix-compress-picture:1.0.15'
//选填 - 视频压缩,开启功能:Phoenix.with().enableCompress(true),获取结果:MediaEntity.getCompressPath()
implementation 'com.github.guoxiaoxing:phoenix-compress-video:1.0.15'

2

Applcation 初始化
public class Applcation extends Application
 {
    @Override
    public void onCreate() {
        super.onCreate();
  
        initPhoenix();
    }

    private void initPhoenix() {
        Phoenix.config()
                .imageLoader(new ImageLoader() {
                    @Override
                    public void loadImage(Context mContext, ImageView imageView
                            , String imagePath, int type) {
                        Glide.with(mContext)
                                .load(imagePath)
                                .into(imageView);
                    }
                });
    }
}

3封装使用aratPickUtils

public class AvaratPickUtils {
    public static void AvatarPick(Activity mContext, int selectCount, int requestCode) {
        Phoenix.with()
                .theme(PhoenixOption.THEME_RED)// 主题
                .fileType(MimeType.ofImage())//显示的文件类型图片、视频、图片和视频
                .maxPickNumber(selectCount)// 最大选择数量
                .minPickNumber(selectCount)// 最小选择数量
                .spanCount(4)// 每行显示个数
                .enablePreview(true)// 是否开启预览
                .enableCamera(true)// 是否开启拍照
                .enableAnimation(true)// 选择界面图片点击效果
                .enableCompress(true)// 是否开启压缩
                .compressPictureFilterSize(1024)//多少kb以下的图片不压缩
                .compressVideoFilterSize(2018)//多少kb以下的视频不压缩
                .thumbnailHeight(160)// 选择界面图片高度
                .thumbnailWidth(160)// 选择界面图片宽度
                .enableClickSound(false)// 是否开启点击声音
                .videoFilterTime(0)//显示多少秒以内的视频
                .mediaFilterSize(0)//显示多少kb以下的图片/视频,默认为0,表示不限制
                //如果是在Activity里使用就传Activity,如果是在Fragment里使用就传Fragment
                .start(mContext, PhoenixOption.TYPE_PICK_MEDIA, requestCode);
    }
}

4在使用的地方

@Override
    public void onClick(View v) {
        switch (v.getId()) {
          
            case R.id.album:
  
               AvaratPickUtils.AvatarPick(UserDataActivity.this, 1, REQUEST_CODE_MHEAD);
  
    }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_MHEAD) {
        // 图片返回
        mHeadList = Phoenix.result(data);
        if (mHeadList != null && mHeadList.size() > 0) {
            String headPath = mHeadList.get(0).getLocalPath();
            Glide.with(this)
                    .load(headPath).transform(new CenterCrop(this), new GlideRoundTransform(this))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .crossFade()
                    .into(ivAvatar);

         File headFile = new File(headPath);
           //上传图像
        }
    }
}
发布了113 篇原创文章 · 获赞 120 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xueshao110/article/details/101557529