您现在的位置是:首页 > 文章详情

android 图片选择,可选择图片,视频,音频,文件,方便扩展

日期:2017-06-11点击:1442

AndroidFilePicker

android file picker ,you can choose image,video,file,etc..
Image selector library for Android. Support single choice、multi-choice、cropping image and preview image.
这里写图片描述
这里写图片描述
这里写图片描述

Quick start

1) Add Library module as a dependency in your build.gradle file.

or

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

allprojects { repositories { ... maven { url 'https://jitpack.io' } } } 
Step 2. Add the dependency
dependencies { compile 'com.github.BrillantZhao:AndroidFilePicker:1.0.2' } 

2) Declare permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3) 图片选择

int mode = selectMode.getCheckedRadioButtonId() == R.id.mode_multiple ? ImagePickActivity.MODE_MULTIPLE : ImagePickActivity.MODE_SINGLE; boolean isShow = showCamera.getCheckedRadioButtonId() == R.id.camera_yes ? true : false; boolean isPreview = enablePreview.getCheckedRadioButtonId() == R.id.preview_enable ? true : false; boolean isCrop = enableCrop.getCheckedRadioButtonId() == R.id.crop_enable ? true : false; ImagePickActivity.start(ImageActivity.this, maxSelectNum, mode, isShow, isPreview, isCrop, ImagePickActivity.REQUEST_IMAGE);

same this

 /** * 启动图片选择页面 * * @param activity * @param maxSelectNum 最大选择图片数量 * @param mode 单选or多选 * @param isShow 是否展示照相机 * @param enablePreview 是否允许预览 * @param enableCrop 是否允许裁剪 * @param requestCode 请求码 */ public static void start(Activity activity, int maxSelectNum, int mode, boolean isShow, boolean enablePreview, boolean enableCrop, int requestCode) { Intent intent = new Intent(activity, ImagePickActivity.class); intent.putExtra(EXTRA_MAX_SELECT_NUM, maxSelectNum); intent.putExtra(EXTRA_SELECT_MODE, mode); intent.putExtra(EXTRA_SHOW_CAMERA, isShow); intent.putExtra(EXTRA_ENABLE_PREVIEW, enablePreview); intent.putExtra(EXTRA_ENABLE_CROP, enableCrop); activity.startActivityForResult(intent, requestCode); }

Receive result in your onActivityResult Method

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == ImagePickActivity.REQUEST_IMAGE) { ArrayList<ImageFile> images = data.getParcelableArrayListExtra(Constant.RESULT_PICK_IMAGE); // do something ... } }

4) 视频选择

boolean isShow = showCamera.getCheckedRadioButtonId() == R.id.camera_yes ? true : false; VideoPickActivity.start(VideoActivity.this, maxSelectNum, isShow,VideoPickActivity.REQUEST_VIDEO);

same this

 /** * 启动视频选择 * * @param activity * @param maxSelectNum 最大选择数量 * @param isShow 是否展示摄像头 * @param requestCode 请求码 */ public static void start(Activity activity, int maxSelectNum, boolean isShow, int requestCode) { Intent intent = new Intent(activity, VideoPickActivity.class); intent.putExtra(EXTRA_MAX_SELECT_NUM, maxSelectNum); intent.putExtra(EXTRA_SHOW_CAMERA, isShow); activity.startActivityForResult(intent, requestCode); }

Receive result in your onActivityResult Method

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == VideoPickActivity.REQUEST_VIDEO) { ArrayList<VideoFile> videoFiles = data.getParcelableArrayListExtra(Constant.RESULT_PICK_VIDEO); // do something... } }

5) 文件选择

String[] mSuffix = {"txt"}; NormalFilePickActivity.start(FileActivity.this, maxSelectNum, mSuffix, NormalFilePickActivity.REQUEST_FILE);

same this

 /** * 启动文件选择 * * @param activity * @param maxSelectNum 最大选择数量 * @param mSuffix 文件格式集合 * @param requestCode 请求码 */ public static void start(Activity activity, int maxSelectNum, String[] mSuffix, int requestCode) { Intent intent = new Intent(activity, NormalFilePickActivity.class); intent.putExtra(EXTRA_MAX_SELECT_NUM, maxSelectNum); intent.putExtra(EXTRA_STUFFIX, mSuffix); activity.startActivityForResult(intent, requestCode); }

Receive result in your onActivityResult Method

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == NormalFilePickActivity.REQUEST_FILE) { ArrayList<NormalFile> normalFiles = data.getParcelableArrayListExtra(Constant.RESULT_PICK_FILE); // do something... } }

5) 音频选择

boolean isNeedRecorder = showCamera.getCheckedRadioButtonId() == R.id.camera_yes ? true : false; AudioPickActivity.start(AudioActivity.this, maxSelectNum, isNeedRecorder, AudioPickActivity.REQUEST_AUDIO);

same this

 /** * 启动录音选择 * * @param activity * @param maxSelectNum 最大选择数量 * @param isNeedRecorder 是否需要录音 * @param requestCode 请求码 */ public static void start(Activity activity, int maxSelectNum, boolean isNeedRecorder, int requestCode) { Intent intent = new Intent(activity, AudioPickActivity.class); intent.putExtra(EXTRA_MAX_SELECT_NUM, maxSelectNum); intent.putExtra(IS_NEED_RECORDER, isNeedRecorder); activity.startActivityForResult(intent, requestCode); }

Receive result in your onActivityResult Method

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == AudioPickActivity.REQUEST_AUDIO) { ArrayList<AudioFile> audioFiles = data.getParcelableArrayListExtra(Constant.RESULT_PICK_VIDEO); // do something... } }

用到的第三方库

Glide
compile ‘com.github.bumptech.glide:glide:3.7.0’
PhotoView
compile ‘com.bm.photoview:library:1.4.1’
//===
compile ‘pub.devrel:easypermissions:0.2.1’
simplecropview
compile ‘com.isseiaoki:simplecropview:1.0.13’

项目地址(欢迎fork):https://github.com/BrillantZhao/AndroidFilePicker

原文链接:https://yq.aliyun.com/articles/120118
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章