Step 1. 在你的根build.gradle文件中增加JitPack仓库依赖。
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
Step 2. 在你的model的build.gradle文件中增加PhotoPicker依赖。
dependencies {
compile 'com.github.jdsjlzx:PhotoPicker:1.0.4'
}
appcompat-v7
version >= 23.0.0
PhotoPicker.builder()
//直接拍照
.setOpenCamera(true)
//拍照后裁剪
.setCrop(true)
//设置裁剪比例(X,Y)
//.setCropXY(1, 1)
//设置裁剪界面标题栏颜色,设置裁剪界面状态栏颜色
//.setCropColors(R.color.colorPrimary, R.color.colorPrimaryDark)
.start(MainActivity.this);
PhotoPicker.builder()
//设置图片选择数量
.setPhotoCount(1)
//取消选择时点击图片浏览
.setPreviewEnabled(false)
//开启裁剪
.setCrop(true)
//设置裁剪比例(X,Y)
.setCropXY(1, 1)
//设置裁剪界面标题栏颜色,设置裁剪界面状态栏颜色
.setCropColors(R.color.colorPrimary, R.color.colorPrimaryDark)
.start(MainActivity.this);
private ArrayList<String> selectedPhotos = new ArrayList<>();
PhotoPicker.builder()
//设置选择个数
.setPhotoCount(9)
//选择界面第一个显示拍照按钮
.setShowCamera(true)
//选择时点击图片放大浏览
.setPreviewEnabled(true)
//附带已经选中过的图片
.setSelected(selectedPhotos)
.start(MainActivity.this);
//多选返回图片后对ArrayList<String> selectedPhotos里的数据操作可达到删除所选择的图片。
//多选选中图片后点击进入大图浏览界面以及标题栏显示删除按钮
PhotoPreview.builder()
//附带已经选中过的图片
.setPhotos(selectedPhotos)
//设置要浏览图片的第position张
.setCurrentItem(position)
.start(MainActivity.this);
//图片浏览,全屏模式
ArrayList<String> imgData = new ArrayList<>();
PhotoPreview.builder()
//设置浏览的图片数据
.setPhotos(imgData)
//设置点击后浏览的是第几张图
.setCurrentItem(position)
//浏览时不要标题栏
//setShowDeleteButton浏览时显示删除按钮.
.setShowToolbar(false)
//开启浏览时长按后显示PopuWindow,分享、保存、取消 等,可以自定义。
.setOnLongClickListData(onLongClickListData)
.start(PreViewImgActivity.this);
使用参考类 //放大预览后可长按图片进行下载、分享、取消(示例)等(自定义)操作,已将选择事件回调回Activity(可根据需求自定义) .setOnLongClickListData(onLongClickListData)
private ArrayList<String> onLongClickListData = new ArrayList<>();
//activity或者fragment里图片浏览时使用
onLongClickListData.add("分享");
onLongClickListData.add("保存");
onLongClickListData.add("取消");
//图片长按后的item点击事件回调
PhotoOnLongClickManager photoOnLongClickManager = PhotoOnLongClickManager.getInstance();
photoOnLongClickManager.setOnLongClickListener(new PhotoOnLongClick() {
@Override
public void sendOnLongClick(int position, String path) {
//自己实现分享或者保存等自定义操作
Toast.makeText(PreViewImgActivity.this, "你点击了:" + onLongClickListData.get(position) + ",图片路径:" + path, Toast.LENGTH_LONG).show();
}
});
//图片浏览API设置
//.setOnLongClickListData(onLongClickListData)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//选择返回
if (resultCode == RESULT_OK &&
(requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
iv_crop.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
List<String> photos = null;
if (data != null) {
photos = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
}
selectedPhotos.clear();
if (photos != null) {
selectedPhotos.addAll(photos);
}
photoAdapter.notifyDataSetChanged();
}
//拍照功能或者裁剪后返回
if (resultCode == RESULT_OK && requestCode == PhotoPicker.CROP_CODE) {
iv_crop.setVisibility(View.VISIBLE);
recyclerView.setVisibility(View.GONE);
Glide.with(getApplicationContext()).load(Uri.fromFile(new File(data.getStringExtra(PhotoPicker.KEY_CAMEAR_PATH)))).into(iv_crop);
}
}
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
...
>
...
<activity
android:name="me.iwf.photopicker.PhotoPickerActivity"
android:theme="@style/customTheme" />
<activity
android:name="me.iwf.photopicker.PhotoPagerActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
</application>
</manifest>
<style name="actionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#fff</item>
<item name="actionBarSize">56dip</item>
</style>
<style name="customTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!--不要修改titleTextColor属性名字->
<item name="titleTextColor">#ffffff</item>
<item name="actionBarTheme">@style/actionBarTheme</item>
<!--设置图片选择界面标题栏以及底栏颜色-->
<item name="colorPrimary">#38393E</item>
<!--设置图片选择界面状态栏颜色-->
<item name="colorPrimaryDark">#2F3034</item>
<item name="actionBarSize">56dip</item>
</style>
# Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# nineoldandroids
-keep interface com.nineoldandroids.view.** { *; }
-dontwarn com.nineoldandroids.**
-keep class com.nineoldandroids.** { *; }
# support-v7-appcompat
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
# support-design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
# ucrop
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
-ignorewarnings
参考:http://blog.csdn.net/jdsjlzx/article/details/68487013
本库是对https://github.com/q805699513/PhotoPicker 的修正,最新版本请访问原始地址。