Skip to content

Commit

Permalink
优化权限申请
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Jun 29, 2019
1 parent dba8aab commit 18fd7e1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Matisse.from(SampleActivity.this)
```java
Matisse.from(SampleActivity.this)
.choose(MimeType.ofAll())
.capture(true)
.capture(true) //默认只能拍照片
//.capture(true, CaptureMode.All)//通过CaptureMode控制拍照照片还是视频,或者都拍
.maxSelectable(9) //默认最大选中9张,设置为1就是单选
.theme(R.style.Matisse_Dracula)//暗色主题
//添加图片过滤器,比如过滤掉小于10K的图片
Expand Down
2 changes: 1 addition & 1 deletion matisse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ publish {
userOrg = 'li-xiaojun'
groupId = 'com.lxj'
artifactId = 'matisse'
publishVersion = '1.0.5'
publishVersion = '1.0.6'
repoName = 'jrepo'
desc = '图片,视频选择和裁剪库,知乎Matisse的增强版。'
website = 'https://github.com/li-xiaojun/Matisse'
Expand Down
15 changes: 13 additions & 2 deletions matisse/src/main/java/com/lxj/matisse/SelectionCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.lxj.matisse;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
Expand Down Expand Up @@ -210,6 +211,13 @@ public SelectionCreator addFilter(@NonNull Filter filter) {
*/
public SelectionCreator capture(boolean enable) {
mSelectionSpec.capture = enable;
mSelectionSpec.captureMode = CaptureMode.Image;
return this;
}

public SelectionCreator capture(boolean enable, CaptureMode captureMode) {
mSelectionSpec.capture = enable;
mSelectionSpec.captureMode = captureMode;
return this;
}

Expand Down Expand Up @@ -360,6 +368,7 @@ public SelectionCreator setOnCheckedListener(@Nullable OnCheckedListener listene
*
* @param requestCode Identity of the request Activity or Fragment.
*/
@SuppressLint("WrongConstant")
public void forResult(final int requestCode) {
final Activity activity = mMatisse.getActivity();
if (activity == null) {
Expand All @@ -373,8 +382,10 @@ public void forResult(final int requestCode) {
xPermission = XPermission.create(activity, PermissionConstants.STORAGE);
} else {
//录制所需权限
xPermission = XPermission.create(activity, PermissionConstants.STORAGE, PermissionConstants.CAMERA,
PermissionConstants.MICROPHONE);
String[] permissions = mSelectionSpec.captureMode == CaptureMode.Image ? new String[]{PermissionConstants.CAMERA}
: new String[]{PermissionConstants.CAMERA, PermissionConstants.MICROPHONE};

xPermission = XPermission.create(activity, permissions);
}
xPermission.callback(new XPermission.SimpleCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class SelectionSpec {
public int originalMaxSize = Integer.MAX_VALUE;
public OnCheckedListener onCheckedListener;
public boolean isCrop;//是否进行裁剪
public CaptureMode captureMode = CaptureMode.All;//默认可以
public CaptureMode captureMode = CaptureMode.Image;//默认可以

private SelectionSpec() {
}
Expand Down Expand Up @@ -94,7 +94,7 @@ private void reset() {
autoHideToobar = true;
originalMaxSize = Integer.MAX_VALUE;
isCrop = false;
captureMode = CaptureMode.All;
captureMode = CaptureMode.Image;
}

public boolean singleSelectionModeEnabled() {
Expand Down
48 changes: 35 additions & 13 deletions matisse/src/main/java/com/lxj/matisse/ui/MatisseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.lxj.matisse.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -38,7 +39,9 @@
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lxj.matisse.CaptureMode;
import com.lxj.matisse.MatisseConst;
import com.lxj.matisse.R;
import com.lxj.matisse.internal.entity.Album;
Expand All @@ -58,7 +61,10 @@
import com.lxj.matisse.internal.utils.MediaStoreCompat;
import com.lxj.matisse.internal.utils.PathUtils;
import com.lxj.matisse.internal.utils.PhotoMetadataUtils;
import com.lxj.xpermission.PermissionConstants;
import com.lxj.xpermission.XPermission;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.util.ArrayList;

Expand Down Expand Up @@ -145,7 +151,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
updateBottomToolbar();

mAlbumsAdapter = new AlbumsAdapter(this, null, false);
albumPopup = new AlbumPopup(this,mAlbumsAdapter,this);
albumPopup = new AlbumPopup(this, mAlbumsAdapter, this);
mAlbumCollection.onCreate(this, this);
mAlbumCollection.onRestoreInstanceState(savedInstanceState);
mAlbumCollection.loadAlbums();
Expand Down Expand Up @@ -184,6 +190,7 @@ public void onBackPressed() {

ArrayList<Uri> selectedUris = new ArrayList<>();
ArrayList<String> selectedPaths = new ArrayList<>();

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -209,7 +216,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
result.putParcelableArrayListExtra(MatisseConst.EXTRA_RESULT_SELECTION, selectedUris);
result.putStringArrayListExtra(MatisseConst.EXTRA_RESULT_SELECTION_PATH, selectedPaths);
result.putExtra(MatisseConst.EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
if(mSpec.isCrop && selected!=null && selected.size()==1 && selected.get(0).isImage()){
if (mSpec.isCrop && selected != null && selected.size() == 1 && selected.get(0).isImage()) {
//开启裁剪
startCrop(this, selected.get(0).uri);
return;
Expand All @@ -229,7 +236,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Just pass the data back to previous calling Activity.
setResult(RESULT_OK, data);
finish();
}else if (requestCode == UCrop.REQUEST_CROP) {
} else if (requestCode == UCrop.REQUEST_CROP) {
final Uri resultUri = UCrop.getOutput(data);
if (resultUri != null) {
//finish with result.
Expand All @@ -242,10 +249,10 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
setResult(RESULT_OK, result);
finish();
} else {
Log.e("Matisse", "ucrop occur error: "+UCrop.getError(data).toString());
Log.e("Matisse", "ucrop occur error: " + UCrop.getError(data).toString());
}
} else if (resultCode == UCrop.RESULT_ERROR) {
Log.e("Matisse", "ucrop occur error: "+UCrop.getError(data).toString());
Log.e("Matisse", "ucrop occur error: " + UCrop.getError(data).toString());
}
}

Expand Down Expand Up @@ -324,10 +331,10 @@ public void onClick(View v) {
ArrayList<String> selectedPaths = (ArrayList<String>) mSelectedCollection.asListOfString();
result.putStringArrayListExtra(MatisseConst.EXTRA_RESULT_SELECTION_PATH, selectedPaths);
result.putExtra(MatisseConst.EXTRA_RESULT_ORIGINAL_ENABLE, mOriginalEnable);
if(mSpec.isCrop && selectedPaths.size()==1 && mSelectedCollection.asList().get(0).isImage()){
if (mSpec.isCrop && selectedPaths.size() == 1 && mSelectedCollection.asList().get(0).isImage()) {
//start crop
startCrop(this, selectedUris.get(0));
}else {
} else {
setResult(RESULT_OK, result);
finish();
}
Expand All @@ -353,8 +360,8 @@ public void onClick(View v) {
}
}

public static void startCrop(Activity context,Uri source){
String destinationFileName = System.nanoTime()+"_crop.jpg";
public static void startCrop(Activity context, Uri source) {
String destinationFileName = System.nanoTime() + "_crop.jpg";
UCrop.Options options = new UCrop.Options();
options.setCompressionQuality(90);
// Color palette
Expand All @@ -372,6 +379,7 @@ public static void startCrop(Activity context,Uri source){
.withOptions(options)
.start(context);
}

//相册条目点击
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Expand Down Expand Up @@ -457,16 +465,30 @@ public SelectedItemCollection provideSelectedItemCollection() {
return mSelectedCollection;
}

@SuppressLint("WrongConstant")
@Override
public void capture() {
// if (mMediaStoreCompat != null) {
// mMediaStoreCompat.dispatchCaptureIntent(this, REQUEST_CODE_CAPTURE);
// }
Intent intent = new Intent(this, CameraActivity.class);
startActivityForResult(intent, MatisseConst.REQUEST_CODE_CAPTURE);

}


String[] permissions = mSpec.captureMode == CaptureMode.Image ? new String[]{PermissionConstants.CAMERA}
: new String[]{PermissionConstants.CAMERA, PermissionConstants.MICROPHONE};

XPermission.create(this, permissions).callback(new XPermission.SimpleCallback() {
@Override
public void onGranted() {
Intent intent = new Intent(MatisseActivity.this, CameraActivity.class);
startActivityForResult(intent, MatisseConst.REQUEST_CODE_CAPTURE);
}

@Override
public void onDenied() {
Toast.makeText(MatisseActivity.this, "没有权限,无法使用该功能", Toast.LENGTH_SHORT).show();
}
}).request();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lxj.matisse.CaptureMode;
import com.lxj.matisse.Matisse;
import com.lxj.matisse.MimeType;
import com.lxj.matisse.engine.impl.PicassoEngine;
Expand Down Expand Up @@ -64,7 +65,8 @@ public void onClick(final View v) {
case R.id.zhihu:
Matisse.from(SampleActivity.this)
.choose(MimeType.ofAll()) //显示所有文件类型,比如图片和视频,
.capture(true)//是否显示拍摄按钮,默认不显示
// .capture(true)//是否显示拍摄按钮,默认不显示
.capture(true, CaptureMode.All)//是否显示拍摄按钮,默认不显示
// .maxSelectable(9) //默认9张
//添加图片过滤器,比如过滤掉小于10K的图片
// .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
Expand Down

0 comments on commit 18fd7e1

Please sign in to comment.