一砸彬、選擇相冊(cè)(FilePicker)
1、導(dǎo)入依賴
implementation 'cn.imlibo:FilePicker:v0.0.5_alpha'
2斯入、使用方法
①選擇指定后綴文件
FilePicker
.from(this)
.chooseForMimeType()//選擇指定后綴文件
.setMaxCount(10)
.setFileTypes("png", "doc","apk", "mp3", "gif", "txt", "mp4", "zip")
.requestCode(REQUEST_CODE_CHOOSE)//確認(rèn)碼
.start();
②在圖片選擇器中選擇圖片或視頻
FilePicker
.from(this)
.chooseMedia()//圖片選擇器
.enabledCapture(true)
.setTheme(R.style.FilePicker_Dracula)
.requestCode(REQUEST_CODE_CHOOSE)
.start();
③接受返回的文件
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_CHOOSE) {
ArrayList<EssFile> essFileList = data.getParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION);
StringBuilder builder = new StringBuilder();
for (EssFile file :
essFileList) {
builder.append(file.getMimeType()).append(" | ").append(file.getName()).append("\n\n");
}
textView.setText(builder.toString());//顯示選取結(jié)果
//setViewText(builder.toString(), pathName);通常上傳的時(shí)候需要文件路徑砂碉,新寫一個(gè)方法保存文件路徑
}
}
3、Git地址
二刻两、拍照(TakePhoto)
①增蹭、添加依賴
repositories {
//glide
mavenCentral()
google()
}
dependencies {
.....
implementation 'com.jph.takephoto:takephoto_library:4.0.3'
//Glide圖片顯示
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}
②、Activity實(shí)現(xiàn)takephoto
/**
* 實(shí)現(xiàn)Takephoto磅摹,InvokeListener滋迈。重寫多個(gè)方法,添加獲取實(shí)例方法getPhoto户誓,對(duì)圖片進(jìn)行壓縮裁剪等操作饼灿。
*/
public class GetPicture extends AppCompatActivity implements TakePhoto.TakeResultListener, InvokeListener {
@BindView(R.id.iv_choose)
ImageView ivChoose;
@BindView(R.id.btn_photo)
Button btnPhoto;
@BindView(R.id.btn_picture)
Button btnPicture;
@BindView(R.id.btn_multiple_choice)
Button btnMultipleChoice;
@BindView(R.id.iv_choose2)
ImageView ivChoose2;
private TakePhoto takePhoto;
private InvokeParam invokeParam;
ArrayList<TImage> images;//返回的圖片對(duì)象
private int flag;//1-->打開相機(jī)(裁剪),2-->打開相冊(cè)(裁剪)帝美,3-->打開相冊(cè)(多選裁剪)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
getTakePhoto().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.get_picture);
ButterKnife.bind(this);
}
/**
* 獲取TakePhoto實(shí)例
*
* @return
*/
public TakePhoto getTakePhoto() {
if (takePhoto == null) {
takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this, this));
}
//設(shè)置壓縮規(guī)則碍彭,最大500kb
takePhoto.onEnableCompress(new CompressConfig.Builder().setMaxSize(500 * 1024).create(), true);
return takePhoto;
}
/**
* 拍照,選取照片等操作
*/
private void takePhotoAndImage() {
//圖片保存路徑
File file = new File(getExternalCacheDir(), System.currentTimeMillis() + ".png");
Uri uri = Uri.fromFile(file);
//圖片裁剪方式
int size = Math.min(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
CropOptions cropOptions = new CropOptions.Builder().setOutputX(size).setOutputX(size).setWithOwnCrop(false).create();
if (flag == 1) {
//相機(jī)獲取照片并剪裁
takePhoto.onPickFromCaptureWithCrop(uri, cropOptions);
//相機(jī)獲取不剪裁
//takePhoto.onPickFromCapture(uri);
} else if (flag == 2) {
//相冊(cè)獲取照片并剪裁
takePhoto.onPickFromGalleryWithCrop(uri, cropOptions);
//相冊(cè)獲取不剪裁
// takePhoto.onPickFromGallery();
} else if (flag == 3) {
//多選,并剪裁
// takePhoto.onPickMultipleWithCrop(3, cropOptions);
//多選庇忌,不剪裁
takePhoto.onPickMultiple(2);
}
}
/**
* 顯示圖片
*/
private void showImg() {
for (int i = 0, j = images.size(); i < j - 1; i += 2) {
Glide.with(this).load(new File(images.get(i).getCompressPath())).into(ivChoose);
Glide.with(this).load(new File(images.get(i + 1).getCompressPath())).into(ivChoose2);
}
if (images.size() % 2 == 1) {
Glide.with(this).load(new File(images.get(images.size() - 1).getCompressPath())).into(ivChoose);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
getTakePhoto().onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
getTakePhoto().onSaveInstanceState(outState);
super.onSaveInstanceState(outState, outPersistentState);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.TPermissionType type = PermissionManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionManager.handlePermissionsResult(this, type, invokeParam, this);
}
@Override
public void takeSuccess(TResult result) {
images = result.getImages();
showImg();
}
@Override
public void takeFail(TResult result, String msg) {
Toast.makeText(CallPhone.this,"獲取圖片失敗",Toast.LENGTH_SHORT).show();
}
@Override
public void takeCancel() {
Toast.makeText(CallPhone.this, "用戶取消操作", Toast.LENGTH_SHORT).show();
}
@Override
public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(this), invokeParam.getMethod());
if (PermissionManager.TPermissionType.WAIT.equals(type)) {
this.invokeParam = invokeParam;
}
return type;
}
@OnClick({R.id.btn_photo, R.id.btn_picture, R.id.btn_multiple_choice})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_photo:
flag = 1;
takePhotoAndImage();
break;
case R.id.btn_picture:
flag = 2;
takePhotoAndImage();
break;
case R.id.btn_multiple_choice:
flag = 3;
takePhotoAndImage();
break;
}
}
}
③實(shí)例圖片
④Git地址