Skip to content

Commit

Permalink
将 newIntent 改成 Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bingoogolapple committed Nov 28, 2017
1 parent 30bf841 commit 3e73916
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 148 deletions.
126 changes: 83 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,51 +70,91 @@ dependencies {

### 2.接口说明

> BGAPhotoPickerActivity
```java
/**
* @param context 应用程序上下文
* @param photoDir 拍照后图片保存的目录。如果传null表示没有拍照功能,如果不为null则具有拍照功能,
* @param maxChooseCount 图片选择张数的最大值
* @param selectedPhotos 当前已选中的图片路径集合,可以传null
* @param pauseOnScroll 滚动列表时是否暂停加载图片
* @return
*/
public static Intent newIntent(Context context, File photoDir, int maxChooseCount, ArrayList<String> selectedPhotos, boolean pauseOnScroll)

/**
* 获取已选择的图片集合
*
* @param intent
* @return
*/
public static ArrayList<String> getSelectedPhotos(Intent intent)
> BGAPhotoPickerActivity - 选择图片
```Java
@AfterPermissionGranted(PRC_PHOTO_PICKER)
private void choicePhotoWrapper() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(this, perms)) {
// 拍照后照片的存放目录,改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerTakePhoto");

Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(this)
.cameraFileDir(mTakePhotoCb.isChecked() ? takePhotoDir : null) // 拍照后照片的存放目录,改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
.maxChooseCount(mPhotosSnpl.getMaxItemCount() - mPhotosSnpl.getItemCount()) // 图片选择张数的最大值
.selectedPhotos(null) // 当前已选中的图片路径集合
.pauseOnScroll(false) // 滚动列表时是否暂停加载图片
.build();
startActivityForResult(photoPickerIntent, RC_CHOOSE_PHOTO);
} else {
EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片\n\n2.拍照", PRC_PHOTO_PICKER, perms);
}
}
```

> BGAPhotoPickerActivity - 获取已选择的图片集合
```Java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == RC_CHOOSE_PHOTO) {
List<String> selectedPhotos = BGAPhotoPickerActivity.getSelectedPhotos(data);
}
}
```

> BGAPhotoPickerPreviewActivity - 预览已选择的图片
```Java
Intent photoPickerPreviewIntent = new BGAPhotoPickerPreviewActivity.IntentBuilder(this)
.previewPhotos(models) // 当前预览的图片路径集合
.selectedPhotos(models) // 当前已选中的图片路径集合
.maxChooseCount(mPhotosSnpl.getMaxItemCount()) // 图片选择张数的最大值
.currentPosition(position) // 当前预览图片的位置
.isFromTakePhoto(false) // 是否是拍完照后跳转过来
.build();
startActivityForResult(photoPickerPreviewIntent, RC_PHOTO_PREVIEW);
```

> BGAPhotoPickerPreviewActivity - 获取预览界面已选择的图片集合
```Java
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PREVIEW) {
// 在预览界面按返回也会回传预览界面已选择的图片集合
List<String> selectedPhotos = BGAPhotoPickerPreviewActivity.getSelectedPhotos(data);
}
}
```

> BGAPhotoPreviewActivity
```java
/**
* 获取查看多张图片的intent
*
* @param context
* @param saveImgDir 保存图片的目录,如果传null,则没有保存图片功能
* @param previewPhotos 当前预览的图片目录里的图片路径集合
* @param currentPosition 当前预览图片的位置
* @return
*/
public static Intent newIntent(Context context, File saveImgDir, ArrayList<String> previewPhotos, int currentPosition)

/**
* 获取查看单张图片的intent
*
* @param context
* @param saveImgDir 保存图片的目录,如果传null,则没有保存图片功能
* @param photoPath 图片路径
* @return
*/
public static Intent newIntent(Context context, File saveImgDir, String photoPath)
> BGAPhotoPreviewActivity - 图片预览
```Java
@AfterPermissionGranted(PRC_PHOTO_PREVIEW)
private void photoPreviewWrapper() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
File downloadDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerDownload");
BGAPhotoPreviewActivity.IntentBuilder photoPreviewIntentBuilder = new BGAPhotoPreviewActivity.IntentBuilder(this)
.saveImgDir(downloadDir); // 保存图片的目录,如果传 null,则没有保存图片功能

if (mCurrentClickNpl.getItemCount() == 1) {
// 预览单张图片
photoPreviewIntentBuilder.previewPhoto(mCurrentClickNpl.getCurrentClickItem());
} else if (mCurrentClickNpl.getItemCount() > 1) {
// 预览多张图片
photoPreviewIntentBuilder.previewPhotos(mCurrentClickNpl.getData())
.currentPosition(mCurrentClickNpl.getCurrentClickItemPosition()); // 当前预览图片的索引
}
startActivity(photoPreviewIntentBuilder.build());
} else {
EasyPermissions.requestPermissions(this, "图片预览需要以下权限:\n\n1.访问设备上的照片", PRC_PHOTO_PREVIEW, perms);
}
}
```

### 3.自定义样式
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
* 你自己项目里「可以不继承 BGAPPToolbarActivity」,我在这里继承 BGAPPToolbarActivity 只是为了方便写 Demo
*/
public class MomentAddActivity extends BGAPPToolbarActivity implements EasyPermissions.PermissionCallbacks, BGASortableNinePhotoLayout.Delegate {
private static final int REQUEST_CODE_PERMISSION_PHOTO_PICKER = 1;
private static final int PRC_PHOTO_PICKER = 1;

private static final int REQUEST_CODE_CHOOSE_PHOTO = 1;
private static final int REQUEST_CODE_PHOTO_PREVIEW = 2;
private static final int RC_CHOOSE_PHOTO = 1;
private static final int RC_PHOTO_PREVIEW = 2;

private static final String EXTRA_MOMENT = "EXTRA_MOMENT";

Expand Down Expand Up @@ -159,24 +159,37 @@ public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePh

@Override
public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
startActivityForResult(BGAPhotoPickerPreviewActivity.newIntent(this, mPhotosSnpl.getMaxItemCount(), models, models, position, false), REQUEST_CODE_PHOTO_PREVIEW);
Intent photoPickerPreviewIntent = new BGAPhotoPickerPreviewActivity.IntentBuilder(this)
.previewPhotos(models) // 当前预览的图片路径集合
.selectedPhotos(models) // 当前已选中的图片路径集合
.maxChooseCount(mPhotosSnpl.getMaxItemCount()) // 图片选择张数的最大值
.currentPosition(position) // 当前预览图片的索引
.isFromTakePhoto(false) // 是否是拍完照后跳转过来
.build();
startActivityForResult(photoPickerPreviewIntent, RC_PHOTO_PREVIEW);
}

@Override
public void onNinePhotoItemExchanged(BGASortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {
Toast.makeText(this, "排序发生变化", Toast.LENGTH_SHORT).show();
}

@AfterPermissionGranted(REQUEST_CODE_PERMISSION_PHOTO_PICKER)
@AfterPermissionGranted(PRC_PHOTO_PICKER)
private void choicePhotoWrapper() {
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(this, perms)) {
// 拍照后照片的存放目录,改成你自己拍照后要存放照片的目录。如果不传递该参数的话就没有拍照功能
File takePhotoDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerTakePhoto");

startActivityForResult(BGAPhotoPickerActivity.newIntent(this, mTakePhotoCb.isChecked() ? takePhotoDir : null, mPhotosSnpl.getMaxItemCount() - mPhotosSnpl.getItemCount(), null, false), REQUEST_CODE_CHOOSE_PHOTO);
Intent photoPickerIntent = new BGAPhotoPickerActivity.IntentBuilder(this)
.cameraFileDir(mTakePhotoCb.isChecked() ? takePhotoDir : null) // 拍照后照片的存放目录,改成你自己拍照后要存放照片的目录。如果不传递该参数的话则不开启图库里的拍照功能
.maxChooseCount(mPhotosSnpl.getMaxItemCount() - mPhotosSnpl.getItemCount()) // 图片选择张数的最大值
.selectedPhotos(null) // 当前已选中的图片路径集合
.pauseOnScroll(false) // 滚动列表时是否暂停加载图片
.build();
startActivityForResult(photoPickerIntent, RC_CHOOSE_PHOTO);
} else {
EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片\n\n2.拍照", REQUEST_CODE_PERMISSION_PHOTO_PICKER, perms);
EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片\n\n2.拍照", PRC_PHOTO_PICKER, perms);
}
}

Expand All @@ -192,21 +205,21 @@ public void onPermissionsGranted(int requestCode, List<String> perms) {

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (requestCode == REQUEST_CODE_PERMISSION_PHOTO_PICKER) {
if (requestCode == PRC_PHOTO_PICKER) {
Toast.makeText(this, "您拒绝了「图片选择」所需要的相关权限!", Toast.LENGTH_SHORT).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_PHOTO) {
if (resultCode == RESULT_OK && requestCode == RC_CHOOSE_PHOTO) {
if (mSingleChoiceCb.isChecked()) {
mPhotosSnpl.setData(BGAPhotoPickerActivity.getSelectedPhotos(data));
} else {
mPhotosSnpl.addMoreData(BGAPhotoPickerActivity.getSelectedPhotos(data));
}
} else if (requestCode == REQUEST_CODE_PHOTO_PREVIEW) {
} else if (requestCode == RC_PHOTO_PREVIEW) {
mPhotosSnpl.setData(BGAPhotoPickerPreviewActivity.getSelectedPhotos(data));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
* BGAOnRVItemClickListener和BGAOnRVItemLongClickListener这两个接口是为了测试事件传递是否正确,你自己的项目里可以不实现这两个接口
*/
public class MomentListActivity extends BGAPPToolbarActivity implements EasyPermissions.PermissionCallbacks, BGANinePhotoLayout.Delegate, BGAOnRVItemClickListener, BGAOnRVItemLongClickListener {
private static final int REQUEST_CODE_PERMISSION_PHOTO_PREVIEW = 1;
private static final int PRC_PHOTO_PREVIEW = 1;

private static final int REQUEST_CODE_ADD_MOMENT = 1;
private static final int RC_ADD_MOMENT = 1;

private RecyclerView mMomentRv;
private MomentAdapter mMomentAdapter;
Expand Down Expand Up @@ -105,7 +105,7 @@ private void addNetImageTestData() {

public void onClick(View v) {
if (v.getId() == R.id.tv_moment_list_add) {
startActivityForResult(new Intent(this, MomentAddActivity.class), REQUEST_CODE_ADD_MOMENT);
startActivityForResult(new Intent(this, MomentAddActivity.class), RC_ADD_MOMENT);
} else if (v.getId() == R.id.tv_moment_list_system) {
startActivity(new Intent(this, SystemGalleryActivity.class));
}
Expand All @@ -114,7 +114,7 @@ public void onClick(View v) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADD_MOMENT) {
if (resultCode == RESULT_OK && requestCode == RC_ADD_MOMENT) {
mMomentAdapter.addFirstItem(MomentAddActivity.getMoment(data));
mMomentRv.smoothScrollToPosition(0);
}
Expand All @@ -123,28 +123,29 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/**
* 图片预览,兼容6.0动态权限
*/
@AfterPermissionGranted(REQUEST_CODE_PERMISSION_PHOTO_PREVIEW)
@AfterPermissionGranted(PRC_PHOTO_PREVIEW)
private void photoPreviewWrapper() {
if (mCurrentClickNpl == null) {
return;
}

// 保存图片的目录,改成你自己要保存图片的目录。如果不传递该参数的话就不会显示右上角的保存按钮
File downloadDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerDownload");

String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
File downloadDir = new File(Environment.getExternalStorageDirectory(), "BGAPhotoPickerDownload");
BGAPhotoPreviewActivity.IntentBuilder photoPreviewIntentBuilder = new BGAPhotoPreviewActivity.IntentBuilder(this)
.saveImgDir(downloadDir); // 保存图片的目录,如果传 null,则没有保存图片功能

if (mCurrentClickNpl.getItemCount() == 1) {
// 预览单张图片

startActivity(BGAPhotoPreviewActivity.newIntent(this, mDownLoadableCb.isChecked() ? downloadDir : null, mCurrentClickNpl.getCurrentClickItem()));
photoPreviewIntentBuilder.previewPhoto(mCurrentClickNpl.getCurrentClickItem());
} else if (mCurrentClickNpl.getItemCount() > 1) {
// 预览多张图片

startActivity(BGAPhotoPreviewActivity.newIntent(this, mDownLoadableCb.isChecked() ? downloadDir : null, mCurrentClickNpl.getData(), mCurrentClickNpl.getCurrentClickItemPosition()));
photoPreviewIntentBuilder.previewPhotos(mCurrentClickNpl.getData())
.currentPosition(mCurrentClickNpl.getCurrentClickItemPosition()); // 当前预览图片的索引
}
startActivity(photoPreviewIntentBuilder.build());
} else {
EasyPermissions.requestPermissions(this, "图片预览需要以下权限:\n\n1.访问设备上的照片", REQUEST_CODE_PERMISSION_PHOTO_PREVIEW, perms);
EasyPermissions.requestPermissions(this, "图片预览需要以下权限:\n\n1.访问设备上的照片", PRC_PHOTO_PREVIEW, perms);
}
}

Expand All @@ -160,7 +161,7 @@ public void onPermissionsGranted(int requestCode, List<String> perms) {

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (requestCode == REQUEST_CODE_PERMISSION_PHOTO_PREVIEW) {
if (requestCode == PRC_PHOTO_PREVIEW) {
Toast.makeText(this, "您拒绝了「图片预览」所需要的相关权限!", Toast.LENGTH_SHORT).show();
}
}
Expand Down
Loading

0 comments on commit 3e73916

Please sign in to comment.