Skip to content

Commit 0dc55e7

Browse files
committedMar 4, 2020
【头像信息修改完善(相机、选图、剪裁模块完成)】
core/util 下新建一个 callback包,下新建CallbackManager、IGlobalCallback、CallbackType; 更新PermissionCheckerDelegate、CameraHandler(注释)、AndroidManifest、UserProfileClickListener;
1 parent 5ed0d2d commit 0dc55e7

File tree

9 files changed

+210
-6
lines changed

9 files changed

+210
-6
lines changed
 
Binary file not shown.
0 Bytes
Binary file not shown.

‎xiaoyun_core/src/main/AndroidManifest.xml

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727

2828
<application
2929
android:allowBackup="true"
30-
android:supportsRtl="true"/>
30+
android:supportsRtl="true">
31+
32+
<!--图片剪裁Activity-->
33+
<activity
34+
android:name="com.yalantis.ucrop.UCropActivity"
35+
android:screenOrientation="portrait"
36+
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
37+
</application>
38+
39+
3140

3241
</manifest>

‎xiaoyun_core/src/main/java/com/lwp/xiaoyun_core/delegates/PermissionCheckerDelegate.java

+68-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
import android.Manifest;
44
import android.content.DialogInterface;
5+
import android.content.Intent;
6+
import android.net.Uri;
57
import android.support.annotation.NonNull;
68
import android.support.v7.app.AlertDialog;
79
import android.widget.Toast;
810

11+
import com.lwp.xiaoyun_core.ui.camera.CameraImageBean;
12+
import com.lwp.xiaoyun_core.ui.camera.RequestCodes;
913
import com.lwp.xiaoyun_core.ui.camera.XiaoYunCamera;
14+
import com.lwp.xiaoyun_core.util.callback.CallbackManager;
15+
import com.lwp.xiaoyun_core.util.callback.CallbackType;
16+
import com.lwp.xiaoyun_core.util.callback.IGlobalCallback;
17+
import com.yalantis.ucrop.UCrop;
1018

1119
import permissions.dispatcher.NeedsPermission;
1220
import permissions.dispatcher.OnNeverAskAgain;
@@ -41,7 +49,9 @@ void startCamera() {
4149
XiaoYunCamera.start(this);
4250
}
4351

44-
//开始请求权限
52+
//开始请求权限,
53+
//在进行 需要权限的操作时!!! 调用;
54+
// 如项目中 点击个人具体信息页的头像时!
4555
public void startCameraWithCheck() {
4656
//开始请求权限(相机、读写)
4757
PermissionCheckerDelegatePermissionsDispatcher.startCameraWithPermissionCheck(this);
@@ -93,4 +103,61 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
93103
//PermissionCheckerDelegatePermissionsDispatcher 是注解自动生成的!!
94104
PermissionCheckerDelegatePermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
95105
}
106+
107+
// 本方法是对应 startActivityForResult()的回调
108+
// 前两个case 与 CameraHandler中的 DELEGATE.startActivityForResult() 相对应
109+
// .
110+
// 后两个case 与 前两个case中的 UCrop.start()相对应,
111+
// 实际上 UCrop.start()源码中,封装了startActivityForResult()
112+
@Override
113+
public void onActivityResult(int requestCode, int resultCode, Intent data) {
114+
super.onActivityResult(requestCode, resultCode, data);
115+
116+
if (resultCode == RESULT_OK) {
117+
118+
switch (requestCode) {
119+
case RequestCodes.TAKE_PHOTO:
120+
//获取到 存储照片的 临时文件路径
121+
final Uri resultUri = CameraImageBean.getInstance().getPath();
122+
UCrop.of(resultUri, resultUri)//一参为 欲剪裁图片的路径,二参为 放置剪切完图片的路径
123+
.withMaxResultSize(400, 400)
124+
.start(getContext(), this);//start()用意看源码
125+
break;
126+
127+
case RequestCodes.PICK_PHOTO:
128+
129+
if (data != null) {
130+
final Uri pickPath = data.getData();//拿到用户选择的图片的路径
131+
132+
//从相册选择后 需要有个路径 来存放 剪裁过的图片
133+
final String pickCropPath = XiaoYunCamera.createCropFile().getPath();
134+
135+
UCrop.of(pickPath, Uri.parse(pickCropPath))
136+
.withMaxResultSize(400, 400)
137+
.start(getContext(), this);
138+
}
139+
break;
140+
141+
case RequestCodes.CROP_PHOTO:
142+
143+
final Uri cropUri = UCrop.getOutput(data);
144+
145+
//拿到剪裁后的数据进行处理
146+
@SuppressWarnings("unchecked")
147+
final IGlobalCallback<Uri> callback = CallbackManager
148+
.getInstance()
149+
.getCallback(CallbackType.ON_CROP);//拿到回调接口
150+
if (callback != null) {
151+
callback.executeCallback(cropUri);//执行回调接口方法
152+
}
153+
break;
154+
155+
case RequestCodes.CROP_ERROR:
156+
Toast.makeText(getContext(), "剪裁出错", Toast.LENGTH_SHORT).show();
157+
break;
158+
default:
159+
break;
160+
}
161+
}
162+
}
96163
}

‎xiaoyun_core/src/main/java/com/lwp/xiaoyun_core/ui/camera/CameraHandler.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
public class CameraHandler implements View.OnClickListener {
3131

3232
private final AlertDialog DIALOG;
33+
//这个Delegate乃是 个人具体信息页 UserProfileDelegate
34+
// (哪个继承自XiaoYunDelegate的 delegate 调用了 PermissionCheckerDelegate.startCameraWithCheck(),
35+
// 这个Delegate就是 哪个,UserProfileDelegate 中的点击事件中,调用了 startCameraWithCheck())
3336
private final PermissionCheckerDelegate DELEGATE;
3437

3538
public CameraHandler(PermissionCheckerDelegate delegate) {
@@ -69,16 +72,20 @@ private String getPhotoName() {
6972
//拍照取图
7073
public void takePhoto() {
7174
final String currentPhotoName = getPhotoName();
75+
7276
//拍照意图
7377
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
7478
//File 临时文件句柄 临时文件:这里是系统相册目录下的当前文件名的文件临时句柄
75-
//CAMERA_PHOTO_DIR 系统相册目录
79+
//CAMERA_PHOTO_DIR 系统相册目录!!!
7680
final File tempFile = new File(FileUtil.CAMERA_PHOTO_DIR, currentPhotoName);
7781

78-
//兼容7.0及以上的写法
82+
//下面是创建临时文件,存储拍的照片
83+
// 兼容7.0及以上的写法
7984
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
85+
8086
final ContentValues contentValues = new ContentValues(1);
8187
contentValues.put(MediaStore.Images.Media.DATA, tempFile.getPath());
88+
8289
//使用 ContentProvider 的方式
8390
final Uri uri = DELEGATE.getContext().getContentResolver().
8491
insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
@@ -87,8 +94,10 @@ public void takePhoto() {
8794
final File realFile =
8895
FileUtils.getFileByPath(FileUtil.getRealFilePath(DELEGATE.getContext(), uri));
8996

97+
//把临时照片文件路径存起来,方便后面使用(如PermissionCheckerDelegate.onActivityResult())
9098
final Uri realUri = Uri.fromFile(realFile);
9199
CameraImageBean.getInstance().setPath(realUri);
100+
//把临时文件Uri路径设置给照片的输出端(EXTRA_OUTPUT)
92101
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
93102
} else {
94103

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.lwp.xiaoyun_core.util.callback;
2+
3+
import java.util.WeakHashMap;
4+
5+
/**
6+
* <pre>
7+
* author : 李蔚蓬(简书_凌川江雪)
8+
* time : 2020/3/5 6:22
9+
* desc : 【回调机制】
10+
* 集成本工具包,可以在任何位置设置回调!!!
11+
* </pre>
12+
*/
13+
public class CallbackManager {
14+
15+
//用来存储所有IGlobalCallback
16+
private static final WeakHashMap<Object, IGlobalCallback> CALLBACKS = new WeakHashMap<>();
17+
18+
//静态内部类型单例模式
19+
private static class Holder {
20+
private static final CallbackManager INSTANCE = new CallbackManager();
21+
}
22+
public static CallbackManager getInstance() {
23+
return Holder.INSTANCE;
24+
}
25+
26+
public CallbackManager addCallback(Object tag, IGlobalCallback callback) {
27+
CALLBACKS.put(tag, callback);
28+
return this;
29+
}
30+
public IGlobalCallback getCallback(Object tag) {
31+
return CALLBACKS.get(tag);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lwp.xiaoyun_core.util.callback;
2+
3+
/**
4+
* <pre>
5+
* author : 李蔚蓬(简书_凌川江雪)
6+
* time : 2020/3/5 6:23
7+
* desc : 为callback本包 提供枚举
8+
* </pre>
9+
*/
10+
public enum CallbackType {
11+
12+
ON_CROP,
13+
TAG_OPEN_PUSH,
14+
TAG_STOP_PUSH,
15+
ON_SCAN,
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lwp.xiaoyun_core.util.callback;
2+
3+
/**
4+
* <pre>
5+
* author : 李蔚蓬(简书_凌川江雪)
6+
* time : 2020/3/5 6:22
7+
* desc : 全局回调
8+
* </pre>
9+
*/
10+
public interface IGlobalCallback<T> {
11+
12+
//泛型接口,一般回调的时候需要给回调方法传递一些值,
13+
// 关于值的类型,这里 指为泛型 比 传入Object好很多,
14+
// 性能、可拓展性、健壮性提高很多
15+
void executeCallback(T args);
16+
}

‎xiaoyun_ec/src/main/java/com/lwp/xiaoyun/ec/main/personal/profile/UserProfileClickListener.java

+56-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22

33
import android.app.AlertDialog;
44
import android.content.DialogInterface;
5+
import android.net.Uri;
56
import android.view.View;
7+
import android.widget.ImageView;
68
import android.widget.TextView;
9+
import android.widget.Toast;
710

11+
import com.alibaba.fastjson.JSON;
12+
import com.bumptech.glide.Glide;
813
import com.chad.library.adapter.base.BaseQuickAdapter;
914
import com.chad.library.adapter.base.listener.SimpleClickListener;
1015
import com.lwp.xiaoyun.ec.R;
1116
import com.lwp.xiaoyun.ec.main.personal.list.ListBean;
1217
import com.lwp.xiaoyun.ui.date.DateDialogUtil;
1318
import com.lwp.xiaoyun_core.delegates.XiaoYunDelegate;
19+
import com.lwp.xiaoyun_core.net.RestClient;
20+
import com.lwp.xiaoyun_core.net.callback.ISuccess;
21+
import com.lwp.xiaoyun_core.util.callback.CallbackManager;
22+
import com.lwp.xiaoyun_core.util.callback.CallbackType;
23+
import com.lwp.xiaoyun_core.util.callback.IGlobalCallback;
24+
import com.lwp.xiaoyun_core.util.log.XiaoYunLogger;
1425

1526
import retrofit2.http.DELETE;
1627

@@ -41,8 +52,51 @@ public void onItemClick(BaseQuickAdapter adapter, final View view, int position)
4152
final int id = bean.getId();//id 来自 UserProfileDelegate!!!
4253
switch (id) {
4354
case 1:
44-
//个人具体信息页面头像点击,启动照相机或选择图片,
45-
// 开始请求权限,
55+
//个人具体信息页面头像点击,启动照相机或选择图片
56+
CallbackManager.getInstance()
57+
.addCallback(CallbackType.ON_CROP, new IGlobalCallback<Uri>() {
58+
@Override
59+
public void executeCallback(Uri args) {
60+
XiaoYunLogger.d("ON_CROP", args);
61+
Toast.makeText(DELEGATE.getContext(), args+"", Toast.LENGTH_SHORT).show();
62+
final ImageView avatar = (ImageView) view.findViewById(R.id.img_arrow_avatar);
63+
Glide.with(DELEGATE)
64+
.load(args)
65+
.into(avatar);
66+
67+
// RestClient.builder()
68+
// .url(UploadConfig.UPLOAD_IMG)
69+
// .loader(DELEGATE.getContext())
70+
// .file(args.getPath())
71+
// .success(new ISuccess() {
72+
// @Override
73+
// public void onSuccess(String response) {
74+
// LatteLogger.d("ON_CROP_UPLOAD", response);
75+
// final String path = JSON.parseObject(response).getJSONObject("result")
76+
// .getString("path");
77+
//
78+
// //通知服务器更新信息
79+
// RestClient.builder()
80+
// .url("user_profile.php")
81+
// .params("avatar", path)
82+
// .loader(DELEGATE.getContext())
83+
// .success(new ISuccess() {
84+
// @Override
85+
// public void onSuccess(String response) {
86+
// //获取更新后的用户信息,然后更新本地数据库
87+
// //没有本地数据的APP,每次打开APP都请求API,获取信息
88+
// }
89+
// })
90+
// .build()
91+
// .post();
92+
// }
93+
// })
94+
// .build()
95+
// .upload();
96+
}
97+
});
98+
99+
// 开始请求权限!!!
46100
// 随后进行权限结果处理——
47101
// 成功则开始选图或拍照,失败则。。详见 PermissionCheckerDelegate
48102
DELEGATE.startCameraWithCheck();

0 commit comments

Comments
 (0)
Please sign in to comment.