https://github.com/bingoogolapple/BGAPhotoPicker-Android
// -------------------- 以下4个库是必须依赖的 ----------------------------
compile 'com.android.support:recyclerview-v7:25.1.0'
compile 'com.jph.takephoto:takephoto_library:4.0.3'
compile 'cn.bingoogolapple:bga-photopicker:1.2.3@aar'
compile 'cn.bingoogolapple:bga-adapter:1.1.8@aar'
compile 'pub.devrel:easypermissions:0.2.1'
// -------------------- 以上4个库是必须依赖的 ----------------------------
package docom.com.choosephotos;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
BGASortableNinePhotoLayout.Delegate, EasyPermissions.PermissionCallbacks {
//
private CheckBox mEditableCb;//是否可编辑
private CheckBox mPlusCb;//是否显示加号
private CheckBox mSortableCb;//是否拖拽排序
//
private CheckBox mTakePhotoCb;//是否开启拍照功能
private BGASortableNinePhotoLayout mPhotosSnpl;//添加照片
//
private TextView tv_cancel;
private TextView tv_determine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
processLogic(savedInstanceState);
}
private void processLogic(Bundle savedInstanceState) {
mEditableCb.setChecked(mPhotosSnpl.isEditable());
mPlusCb.setChecked(mPhotosSnpl.isPlusEnable());
mSortableCb.setChecked(mPhotosSnpl.isSortable());
}
private void setListener() {
mEditableCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mPhotosSnpl.setEditable(checked);
}
});
mPlusCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mPhotosSnpl.setPlusEnable(checked);
}
});
mSortableCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
mPhotosSnpl.setSortable(checked);
}
});
// 设置拖拽排序控件的代理
mPhotosSnpl.setDelegate(this);
}
private void initView() {
//
mEditableCb = (CheckBox) findViewById(R.id.cb_moment_add_editable);
mPlusCb = (CheckBox) findViewById(R.id.cb_moment_add_plus);
mSortableCb = (CheckBox) findViewById(R.id.cb_moment_add_sortable);
//
mTakePhotoCb = (CheckBox) findViewById(R.id.cb_moment_add_take_photo);//是否开启拍照
mPhotosSnpl = (BGASortableNinePhotoLayout) findViewById(R.id.snpl_moment_add_photos);//添加照片
//
tv_cancel = (TextView) findViewById(R.id.tv_cancel);
tv_cancel.setOnClickListener(this);
tv_determine = (TextView) findViewById(R.id.tv_determine);
tv_determine.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_cancel:
finish();
break;
case R.id.tv_determine:
break;
}
}
//选择照片 TODO start
private static final int REQUEST_CODE_PERMISSION_PHOTO_PICKER = 1;
private static final int REQUEST_CODE_CHOOSE_PHOTO = 1;
private static final int REQUEST_CODE_PHOTO_PREVIEW = 3;
@Override
public void onClickAddNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
choicePhotoWrapper();
}
@AfterPermissionGranted(REQUEST_CODE_PERMISSION_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);
} else {
EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问设备上的照片\n\n2.拍照",
REQUEST_CODE_PERMISSION_PHOTO_PICKER, perms);
}
}
@Override
public void onClickDeleteNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
mPhotosSnpl.removeItem(position);
}
@Override
public void onClickNinePhotoItem(BGASortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
}
//选择照片 TODO end
//期限申请
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (requestCode == REQUEST_CODE_PERMISSION_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) {
mPhotosSnpl.addMoreData(BGAPhotoPickerActivity.getSelectedImages(data));
} else if (requestCode == REQUEST_CODE_PHOTO_PREVIEW) {
mPhotosSnpl.setData(BGAPhotoPickerPreviewActivity.getSelectedImages(data));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/mian_74"
android:background="#dd4437"
android:gravity="center"
android:text="白名单上传"
android:textColor="#fff"
android:textSize="@dimen/main_28"/>
<LinearLayout
style="@style/MatchWrap.Horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="gone"
tools:visibility="visible">
<CheckBox
android:id="@+id/cb_moment_add_editable"
style="@style/AutoWrap"
android:checked="true"
android:text="是否可编辑"
android:textColor="@color/colorPrimary"
android:textSize="12sp"/>
<CheckBox
android:id="@+id/cb_moment_add_plus"
style="@style/AutoWrap"
android:checked="true"
android:text="是否显示加号"
android:textColor="@color/colorPrimary"
android:textSize="12sp"/>
<CheckBox
android:id="@+id/cb_moment_add_sortable"
style="@style/AutoWrap"
android:checked="true"
android:text="是否拖拽排序"
android:textColor="@color/colorPrimary"
android:textSize="12sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/main_20"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/main_10"
android:text="添加白名单"
android:textColor="#485465"
android:textSize="@dimen/main_20"/>
<CheckBox
android:id="@+id/cb_moment_add_take_photo"
style="@style/AutoWrap"
android:checked="true"
android:text="是否开启拍照功能"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/main_20"
android:visibility="gone"
tools:visibility="visible"/>
</LinearLayout>
<cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout
android:id="@+id/snpl_moment_add_photos"
style="@style/WrapWrap"
android:layout_margin="10dp"
app:bga_snpl_deleteDrawable="@mipmap/bga_pp_ic_delete"
app:bga_snpl_deleteDrawableOverlapQuarter="false"
app:bga_snpl_editable="true"
app:bga_snpl_itemCornerRadius="0dp"
app:bga_snpl_itemSpanCount="3"
app:bga_snpl_itemWhiteSpacing="4dp"
app:bga_snpl_itemWidth="0dp"
app:bga_snpl_maxItemCount="3"
app:bga_snpl_otherWhiteSpacing="100dp"
app:bga_snpl_placeholderDrawable="@mipmap/bga_pp_ic_holder_dark"
app:bga_snpl_plusDrawable="@mipmap/bga_pp_ic_plus"
app:bga_snpl_plusEnable="true"
app:bga_snpl_sortable="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/main_56">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/main_56"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="@dimen/main_57"
android:layout_marginLeft="@dimen/main_39"
android:layout_marginRight="@dimen/main_39"
android:layout_marginTop="@dimen/main_10"
android:gravity="center">
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/setting_cancel_selected"
android:paddingBottom="@dimen/main_10"
android:paddingLeft="@dimen/main_30"
android:paddingRight="@dimen/main_30"
android:paddingTop="@dimen/main_10"
android:text="取消"
android:textColor="#fff"
android:textSize="@dimen/main_24"/>
<TextView
android:id="@+id/tv_determine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/main_24"
android:background="@drawable/setting_cancel_selected"
android:paddingBottom="@dimen/main_10"
android:paddingLeft="@dimen/main_30"
android:paddingRight="@dimen/main_30"
android:paddingTop="@dimen/main_10"
android:text="确定"
android:textColor="#fff"
android:textSize="@dimen/main_24"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
拍照,选择照片
最新推荐文章于 2024-04-01 00:27:18 发布