|
|
@@ -0,0 +1,183 @@
|
|
|
+package com.sheep.jiuyan.samllsheep.base;
|
|
|
+
|
|
|
+import android.content.Intent;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.kfzs.appstore.utils.string.HexUtils;
|
|
|
+import com.kfzs.duanduan.react.upfile.UpFileListener;
|
|
|
+import com.kfzs.duanduan.react.upfile.UpFileUtils;
|
|
|
+import com.lzy.imagepicker.ImagePicker;
|
|
|
+import com.lzy.imagepicker.bean.ImageItem;
|
|
|
+import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+import com.sheep.gamegroup.view.dialog.DialogChooseImage;
|
|
|
+import com.sheep.gamegroup.view.dialog.DialogShowLoading;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+import go.kfzssafe.Kfzssafe;
|
|
|
+import rx.android.schedulers.AndroidSchedulers;
|
|
|
+import rx.functions.Action1;
|
|
|
+import rx.schedulers.Schedulers;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2018/3/26.
|
|
|
+ * realicing@sina.com
|
|
|
+ */
|
|
|
+
|
|
|
+public abstract class AbsChooseImageActivity extends BaseActivity implements UpFileListener{
|
|
|
+ public static final int REQUEST_CODE_SELECT = 113;
|
|
|
+ public static final int REQUEST_CODE_CAMERA = 114;
|
|
|
+ private boolean isUpload = true;
|
|
|
+ private DialogShowLoading dialogShowLoading;
|
|
|
+ private ImageItem imageItem;
|
|
|
+ public void showChooseDialog(){
|
|
|
+ DialogChooseImage.showDialog(this);
|
|
|
+ }
|
|
|
+ public void showChooseDialog(boolean isUpload){
|
|
|
+ this.isUpload = isUpload;
|
|
|
+ DialogChooseImage.showDialog(this);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
|
|
|
+ if (data != null && (requestCode == REQUEST_CODE_SELECT||requestCode == REQUEST_CODE_CAMERA)) {
|
|
|
+ ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
|
|
|
+ if(images != null && !images.isEmpty() && (imageItem = images.get(0)) != null && !TextUtils.isEmpty(imageItem.path)) {
|
|
|
+ if(isUpload){
|
|
|
+ AbsChooseImageActivity.this.dialogShowLoading = DialogShowLoading.showDialog(AbsChooseImageActivity.this);
|
|
|
+ UpFileUtils.upImage(new File(images.get(0).path), AbsChooseImageActivity.this);
|
|
|
+ } else {
|
|
|
+ onGetImage(imageItem);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ onNotGetImage("没有数据");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void Success(String msg) {
|
|
|
+ //{data: ""}
|
|
|
+
|
|
|
+ String json = null;
|
|
|
+ if(TextUtils.isEmpty(msg)){
|
|
|
+ updateError("失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(msg.startsWith("{")){
|
|
|
+ json = msg;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ byte[] bytes = HexUtils.hexStr2Bytes(msg);
|
|
|
+ byte[] byteDecode = Kfzssafe.XByteDecode(bytes);
|
|
|
+ json = new String(byteDecode);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ updateError("失败");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ UploadResult uploadResult = JSON.parseObject(json, UploadResult.class);
|
|
|
+ if(uploadResult != null){
|
|
|
+ String data = uploadResult.getData().getUrl();
|
|
|
+ dialogShowLoading.getTextView().setText("更新中");
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("avatar", data);
|
|
|
+ SheepApp.get(this).getNetComponent().getApiService().changeBaseInfo(jsonObject)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new Action1<BaseMessage>() {
|
|
|
+ @Override
|
|
|
+ public void call(BaseMessage baseMessage) {
|
|
|
+ dialogShowLoading.getTextView().setText("完成");
|
|
|
+ onGetImage(imageItem);
|
|
|
+ dialogShowLoading.getAlertDialog().dismiss();
|
|
|
+ }
|
|
|
+ }, new Action1<Throwable>() {
|
|
|
+ @Override
|
|
|
+ public void call(Throwable throwable) {
|
|
|
+ updateError("失败");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateError(String msg) {
|
|
|
+ dialogShowLoading.getTextView().setText(msg);
|
|
|
+ onNotGetImage(msg);
|
|
|
+ dialogShowLoading.getAlertDialog().dismiss();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void Progress(String progress) {
|
|
|
+ dialogShowLoading.getTextView().setText(progress);
|
|
|
+ dialogShowLoading.getTextView().append("%");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void Failure(String err) {
|
|
|
+ onNotGetImage(err);
|
|
|
+ }
|
|
|
+ protected abstract void onNotGetImage(String msg);
|
|
|
+
|
|
|
+ protected abstract void onGetImage(ImageItem image);
|
|
|
+
|
|
|
+}
|
|
|
+class Data{
|
|
|
+ private String url;
|
|
|
+
|
|
|
+ public String getUrl() {
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUrl(String url) {
|
|
|
+ this.url = url;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+class UploadResult{
|
|
|
+ private int status;
|
|
|
+ private String error;
|
|
|
+ private String error_message;
|
|
|
+ private Data data;
|
|
|
+
|
|
|
+ public int getStatus() {
|
|
|
+ return status;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setStatus(int status) {
|
|
|
+ this.status = status;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getError() {
|
|
|
+ return error;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setError(String error) {
|
|
|
+ this.error = error;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getError_message() {
|
|
|
+ return error_message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setError_message(String error_message) {
|
|
|
+ this.error_message = error_message;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Data getData() {
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setData(Data data) {
|
|
|
+ this.data = data;
|
|
|
+ }
|
|
|
+}
|