一个Android本地图片视频选择器,视频支持预览和缩略图。
把demo中的medialib作为一个module导入你的工程。
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
// For developers using AndroidX in their applications
implementation 'com.github.cmcy:media-selector:#lastVersion'
// For developers using the Android Support Library
implementation 'com.github.cmcy:media-selector:1.1.1'
}
如果项目没有配置FileProvider,则需要配置一下
<application
...
android:requestLegacyExternalStorage="true">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
android 10.0需要在application加上android:requestLegacyExternalStorage="true"
MediaSelector.get(this)
.showCamera(true)//默认显示,可以不用设置
.setSelectMode(MediaSelector.MODE_MULTI)//默认多选
.setMaxCount(20)//默认最多选择5张,设置单选后此设置无效
.setMediaType(MediaSelector.PICTURE)//默认选择图片
.setDefaultList(imageAdapter.getSelect())//默认选中的图片/视频
.setListener(new MediaSelectorListener())//选择完成的回调, (可以设置回调或者用onActivityResult方式接收)
.jump();
使用回调的方式接收返回图片/视频
.setListener(new MediaSelector.MediaSelectorListener() {
@Override
public void onMediaResult(List<String> resultList) {
//选择的图片/视频
}
})
使用onActivityResult方式接收返回图片/视频
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == MediaSelector.REQUEST_IMAGE && resultCode == RESULT_OK){
List<String> resultList = data.getStringArrayListExtra(MediaSelector.EXTRA_RESULT);
Log.e("TAG", "size-->" + resultList.size());
}
}
https://blog.csdn.net/u012364659/article/details/106523681
Copyright 2019 sendtion
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.