diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/AccountInfoActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/AccountInfoActivity.java index c28e738ae5c08dff58a16e7bc6b72b6b551f3682..514030f3795964435af6c5ebecccad30e9839e09 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/AccountInfoActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/AccountInfoActivity.java @@ -17,12 +17,19 @@ package com.mindspore.himindspore.ui.me; import android.app.DatePickerDialog; import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Build; +import android.provider.MediaStore; import android.text.TextUtils; import android.util.Log; +import android.view.View; import android.widget.ImageView; +import android.widget.LinearLayout; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.core.content.FileProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -30,7 +37,11 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; import com.mindspore.common.base.mvp.BaseActivity; +import com.mindspore.common.netbean.request.UploadBean; import com.mindspore.common.netbean.response.AccountBean; +import com.mindspore.common.netbean.response.UploadResBean; +import com.mindspore.common.utils.BitmapUtils; +import com.mindspore.common.utils.UriUtils; import com.mindspore.customview.ui.AccountViewBean; import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; @@ -43,6 +54,8 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; @@ -59,8 +72,13 @@ public class AccountInfoActivity extends BaseActivity private final String SEX_MAN_STR = "男"; private final String SEX_WOMAN_STR = "女"; private final String SEX_SECRET_STR = "保密"; - private String[] sexOption = new String[]{SEX_MAN_STR, SEX_WOMAN_STR, SEX_SECRET_STR}; + private final String AVATAR_FROM_CAMERA = "拍照"; + private final String AVATAR_FROM_GALlERY = "图库"; + private static int RESULT_LOAD_IMAGE_BY_GALLERY = 10; + private static int RESULT_LOAD_IMAGE_BY_CAMERA = 11; + private String[] sexOption = new String[]{SEX_MAN_STR, SEX_WOMAN_STR, SEX_SECRET_STR}; + private String[] avatarOption = new String[]{AVATAR_FROM_CAMERA, AVATAR_FROM_GALlERY}; private ImageView avatar; private ArrayList mList = new ArrayList<>(); private AccountBean accountBean = new AccountBean(); @@ -69,7 +87,11 @@ public class AccountInfoActivity extends BaseActivity AccountViewBean sexViewBean; AccountViewBean birthdayViewBean; AccountViewBean personalSignatureVb; + LinearLayout avatarModifyLayout; + LinearLayout loadingLayout; + private UserInfoAdapter infoAdapter; + private Uri imageUri; @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(AccountChangedEvent event) { @@ -83,19 +105,55 @@ public class AccountInfoActivity extends BaseActivity @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + Log.d(TAG, "onActivityResult requestCode:" + requestCode + "resultCode:" + resultCode); super.onActivityResult(requestCode, resultCode, data); if (accountBean != null) { if (requestCode == ModifyStringActivity.MODIFY_NICKNAME_CODE && resultCode == RESULT_OK) { accountBean.setNickname(data.getStringExtra(ModifyStringActivity.MODIFY_CONTENT)); + presenter.updateAccount(accountBean); } else if (requestCode == ModifyStringActivity.MODIFY_SIGNATURES_CODE && resultCode == RESULT_OK) { accountBean.setSignature(data.getStringExtra(ModifyStringActivity.MODIFY_CONTENT)); + presenter.updateAccount(accountBean); + } else if (requestCode == RESULT_LOAD_IMAGE_BY_GALLERY && resultCode == RESULT_OK + && null != data) { + Uri selectedImage = data.getData(); + String[] filePathColumn = {MediaStore.Images.Media.DATA}; + //查询我们需要的数据 + Cursor cursor = getContentResolver().query(selectedImage, + filePathColumn, null, null, null); + cursor.moveToFirst(); + int columnIndex = cursor.getColumnIndex(filePathColumn[0]); + String picturePath = cursor.getString(columnIndex); + if (!BitmapUtils.isImageFile(picturePath)) { + MyToast.MakeText(getString(R.string.get_avatar_not_image), 1000); + return; + } + Log.d(TAG, "GALLERY picturePath: " + picturePath); + uploadImage(picturePath); + cursor.close(); + } else if (requestCode == RESULT_LOAD_IMAGE_BY_CAMERA) { + Log.d(TAG, "CAMERA imageUri: " + imageUri.toString()); + String picturePath = UriUtils.getFilePathFromUri(imageUri, this); + Log.d(TAG, "CAMERA picturePath: " + picturePath); + uploadImage(picturePath); } else { - Log.e(TAG, "ModifyStringActivity result is error"); + Log.e(TAG, "onActivityResult requestCode is error"); return; } - presenter.updateAccount(accountBean); + } + } + + private void uploadImage(String picturePath) { + if (!TextUtils.isEmpty(picturePath)) { + String uploadImage = BitmapUtils.imageToBase64(picturePath); + Log.d(TAG, "uploadImage.length: " + uploadImage.length()); + // 调用网络接口上传 uploadImage + presenter.uploadAvatar(new UploadBean(uploadImage)); + } else { + Log.e(TAG, "get picture failed."); + MyToast.MakeText(getString(R.string.get_avatar_fail), 1000); } } @@ -103,7 +161,13 @@ public class AccountInfoActivity extends BaseActivity public void init() { EventBus.getDefault().register(this); presenter = new AccountInfoPresenter(this); - presenter.getAccount(); + + avatar = findViewById(R.id.account_avatar_image); + avatarModifyLayout = findViewById(R.id.avatar_item_area); + avatarModifyLayout.setOnClickListener(v -> { + setAvatarDialog(); + }); + nicknameViewBean = new AccountViewBean(getResources().getString(R.string.user_nick_name), ""); sexViewBean = new AccountViewBean(getResources().getString(R.string.user_sexes), ""); birthdayViewBean = new AccountViewBean(getResources().getString(R.string.user_age), ""); @@ -144,12 +208,62 @@ public class AccountInfoActivity extends BaseActivity mList.add(sexViewBean); mList.add(birthdayViewBean); mList.add(personalSignatureVb); - avatar = findViewById(R.id.account_avatar_image); + RecyclerView mRecyclerView = findViewById(R.id.recyclerView); infoAdapter = new UserInfoAdapter(AccountInfoActivity.this, mList); LinearLayoutManager manager = new LinearLayoutManager(AccountInfoActivity.this, LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(manager); mRecyclerView.setAdapter(infoAdapter); + loadingLayout = findViewById(R.id.loading_layout); + presenter.getAccount(); + } + + private void setAvatarDialog() { + AlertDialog ad = new AlertDialog.Builder(this, R.style.RadioDialogTheme) + .setTitle(getString(R.string.update_avatar)) + .setSingleChoiceItems(avatarOption, 0, + (dialog, which) -> { + if (which == 0) { + // 开启相机 + takePhoto(); + } else if (which == 1) { + //点击事件,而重定向到图片库 + Intent intent = new Intent(Intent.ACTION_PICK, null); + intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + "image/*"); + startActivityForResult(intent, RESULT_LOAD_IMAGE_BY_GALLERY); + } + dialog.dismiss(); + }) + .create(); + ad.show(); + } + + /** + * 拍照 + */ + private void takePhoto() { + Log.e(TAG, "takePhoto"); + // 创建File对象,用于存储拍照后的图片 + File outputImage = new File(getExternalCacheDir(), "output_image.jpg"); + try { + if (outputImage.exists()) { + outputImage.delete(); + } + outputImage.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + if (Build.VERSION.SDK_INT < 24) { + imageUri = Uri.fromFile(outputImage); + } else { + imageUri = FileProvider.getUriForFile(this, + getPackageName() + ".fileprovider", outputImage); + } + // 启动相机程序 + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); + startActivityForResult(intent, RESULT_LOAD_IMAGE_BY_CAMERA); } private void setSexDialog() { @@ -213,12 +327,12 @@ public class AccountInfoActivity extends BaseActivity @Override public void showLoading() { - + loadingLayout.setVisibility(View.VISIBLE); } @Override public void dismissLoading() { - + loadingLayout.setVisibility(View.GONE); } @Override @@ -259,4 +373,15 @@ public class AccountInfoActivity extends BaseActivity super.onDestroy(); EventBus.getDefault().unregister(this); } + + @Override + public void getResponseUploadData(@NonNull UploadResBean uploadResBean) { + if (accountBean != null) { + Log.d(TAG, "uploadResBean.getUrl:" + uploadResBean.getUrl()); + accountBean.setAvatar(uploadResBean.getUrl()); + presenter.updateAccount(accountBean); + } else { + Log.e(TAG, "accountBean is null. can`t modify account."); + } + } } \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/LoginActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/LoginActivity.java index c2cb099683e7743ec82d805f364e682f0c8902c6..25ce68b3dd9d1046aafe4d3bb611d575df1aea74 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/LoginActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/LoginActivity.java @@ -14,7 +14,7 @@ import com.mindspore.common.utils.PhoneFormatCheckUtils; import com.mindspore.common.utils.UserUtils; import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; -import com.mindspore.himindspore.ui.me.event.LoginStateChangedEvent; +import com.mindspore.common.net.event.LoginStateChangedEvent; import com.mindspore.himindspore.ui.me.mvp.LoginContract; import com.mindspore.himindspore.ui.me.mvp.LoginPresenter; diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java index 445034eb0415f5d0b0e3de39fcc48cfad872a4c2..1ba7cb9dc613e42980576ea9b780203ac92b1488 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java @@ -41,6 +41,9 @@ import androidx.core.content.FileProvider; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.bumptech.glide.request.RequestOptions; import com.mindspore.common.base.mvp.BaseFragment; import com.mindspore.common.config.MSLinkUtils; import com.mindspore.common.net.download.DownloadInfo; @@ -56,7 +59,7 @@ import com.mindspore.himindspore.ui.me.bean.ServerInfoBean; import com.mindspore.himindspore.ui.me.mvp.PersonalPresenter; import com.mindspore.himindspore.R; import com.mindspore.himindspore.ui.me.event.AccountChangedEvent; -import com.mindspore.himindspore.ui.me.event.LoginStateChangedEvent; +import com.mindspore.common.net.event.LoginStateChangedEvent; import com.mindspore.himindspore.ui.webView.WebViewUtilsActivity; import java.io.File; @@ -189,6 +192,10 @@ public class PersonalFragment extends BaseFragment implements attentionTv.setText("-"); fansTv.setText("-"); taskLayout.setVisibility(View.GONE); + Glide.with(this) + .load(R.drawable.icon_avatar_default) + .apply(RequestOptions.bitmapTransform(new RoundedCorners(90))) + .into(avatarIv); } } @@ -437,6 +444,13 @@ public class PersonalFragment extends BaseFragment implements dynamicTv.setText(responseBean.getDynamic() + ""); attentionTv.setText(responseBean.getAttention() + ""); fansTv.setText(responseBean.getFansNumber() + ""); + Glide.with(this) + .load(responseBean.getAvatar()) + .apply(RequestOptions.bitmapTransform(new RoundedCorners(90))) + .error(R.drawable.icon_avatar_default) + .placeholder(R.drawable.icon_avatar_default) + .into(avatarIv); + List taskList = responseBean.getTaskEntityList(); if (taskList != null && !taskList.isEmpty()) { taskLayout.setVisibility(View.VISIBLE); diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/SetActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/SetActivity.java index 9140596019bfdccb34a853f4a5f0287d17652661..44d0d979e578af08c22d401c735086fad71bb6b6 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/SetActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/SetActivity.java @@ -31,7 +31,7 @@ import com.mindspore.common.utils.UserUtils; import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; import com.mindspore.himindspore.R.id; -import com.mindspore.himindspore.ui.me.event.LoginStateChangedEvent; +import com.mindspore.common.net.event.LoginStateChangedEvent; import com.mindspore.himindspore.ui.me.mvp.LoginContract; import com.mindspore.himindspore.ui.me.mvp.LogoutPresenter; import com.mindspore.himindspore.ui.webView.WebViewUtilsActivity; diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountContract.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountContract.java index e920da51a2aab582681815af2b0c670bbbacee0f..3b200baf3e27b9b8b8ddf01eec0208e1e0b9f40d 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountContract.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountContract.java @@ -1,13 +1,18 @@ package com.mindspore.himindspore.ui.me.mvp; +import com.mindspore.common.netbean.request.UploadBean; import com.mindspore.common.netbean.response.AccountBean; +import com.mindspore.common.netbean.response.UploadResBean; import com.mindspore.himindspore.ui.view.BaseContractView; public interface AccountContract { - interface AccountView extends BaseContractView {} + interface AccountView extends BaseContractView { + void getResponseUploadData(UploadResBean uploadResBean); + } interface AccountViewPresenter { void getAccount(); void updateAccount(AccountBean accountBean); + void uploadAvatar(UploadBean uploadBean); } } diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountInfoPresenter.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountInfoPresenter.java index 67ef64190c37085f8001cad8634052ef9c4327e7..3ded6b4340655d5fdf6b9fe88e51017bde2ee5d9 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountInfoPresenter.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/mvp/AccountInfoPresenter.java @@ -7,7 +7,9 @@ import androidx.annotation.NonNull; import com.mindspore.common.base.mvp.BaseActivityPresenter; import com.mindspore.common.http.HttpClient; import com.mindspore.common.net.BaseObserver; +import com.mindspore.common.netbean.request.UploadBean; import com.mindspore.common.netbean.response.AccountBean; +import com.mindspore.common.netbean.response.UploadResBean; import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; import com.mindspore.himindspore.ui.me.AccountInfoActivity; @@ -37,6 +39,9 @@ public class AccountInfoPresenter extends BaseActivityPresenter>" + errorCode); MyToast.MakeText(msg, 1000); + if (view != null) { + view.dismissLoading(); + } } @Override public void onNext(@NonNull AccountBean responseBean) { Log.d(TAG, "getAccount.onNext."); if (view != null) { + view.dismissLoading(); view.getResponseData(responseBean); } } @@ -77,6 +86,9 @@ public class AccountInfoPresenter extends BaseActivityPresenter>" + errorCode); MyToast.MakeText(msg, 1000); + if (view != null) { + view.dismissLoading(); + } } @Override public void onNext(@NonNull ResponseBody responseBean) { Log.d(TAG, "updateAccount.onNext."); EventBus.getDefault().post(new AccountChangedEvent()); + if (view != null) { + view.dismissLoading(); + } MyToast.MakeText(view.getString(R.string.modify_success), 1000); } @@ -107,6 +125,54 @@ public class AccountInfoPresenter extends BaseActivityPresenter() { + + @Override + protected void onStart() { + super.onStart(); + if (view != null) { + view.showLoading(); + } + } + + @Override + public void onFailed(String msg, int errorCode) { + super.onFailed(msg, errorCode); + Log.e(TAG, "uploadAvatar.onFailed, Throwable:" + msg + "errorCode>>" + errorCode); + if (view != null) { + view.dismissLoading(); + } + MyToast.MakeText(msg, 1000); + } + + @Override + public void onNext(@NonNull UploadResBean responseBean) { + Log.d(TAG, "uploadAvatar.onNext."); + if (view != null) { + view.getResponseUploadData(responseBean); + view.dismissLoading(); + } + MyToast.MakeText(view.getString(R.string.upload_success), 1000); + } + + @Override + public void onError(@NonNull Throwable e) { + super.onError(e); + String message = e.getMessage(); + Log.e(TAG, "uploadAvatar.onError, Throwable:" + message); + } + + @Override + public void onComplete() { + Log.d(TAG, "uploadAvatar.onComplete."); + } + }); + } + @Override public void clear() { super.clear(); diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account.xml index 14aab1dc71eb79fe2cb5551719351fe84d1bd902..19fc4b771f51febfd56f6b0e4a4243cfcf12bd60 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account.xml @@ -1,5 +1,5 @@ - + android:orientation="horizontal" + app:layout_constraintTop_toBottomOf="@+id/account_app_title" + app:layout_constraintStart_toStartOf="parent"> - + android:layout_marginStart="@dimen/spaceHorizontalSize" + app:layout_constraintTop_toBottomOf="@+id/avatar_item_area" + app:layout_constraintStart_toStartOf="parent"/> + android:layout_height="wrap_content" + app:layout_constraintTop_toBottomOf="@+id/line_view" + app:layout_constraintStart_toStartOf="parent"/> + + + + + + - \ No newline at end of file + \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account_modify_string_data.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account_modify_string_data.xml index b3bb77f0cb4e03d5e503aa8d8c1295fb9438214b..20f86a3ed1ae261c070648a81c52ee14b84026cd 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account_modify_string_data.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_account_modify_string_data.xml @@ -5,8 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" - android:orientation="vertical" - android:paddingHorizontal="@dimen/spaceHorizontalSize"> + android:orientation="vertical"> + android:textColor="@color/white" /> \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml index 83ff289a7a6d7c8fd3c6982b8be8a250ccf7ffaa..dc78b1ca2c9cfc2b4c8a04e3f9ca9b44e4e9131c 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml @@ -71,6 +71,7 @@ 版本号 修改 修改成功 + 上传成功 同意 不同意并退出 《用户协议和隐私政策》 @@ -219,9 +220,12 @@ 我的关注 我的粉丝 头像 + 获取照片失败 + 您选择的不是图片 昵称 性别 选择性别 + 头像选择 修改出生日期 不能超过当前日期 出生年月 diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/exception/ExceptionHandle.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/exception/ExceptionHandle.java index 8c99fca8fb96a53660b842abf881e808f6f559f7..dbdfdaab4227267b9964d49ab9c489147385efa1 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/exception/ExceptionHandle.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/exception/ExceptionHandle.java @@ -36,6 +36,8 @@ public class ExceptionHandle { HttpException httpException = (HttpException) e; switch (httpException.code()) { case UNAUTHORIZED: + errorMsg = "token 失效,请重新登录"; + break; case FORBIDDEN: case NOT_FOUND: case REQUEST_TIMEOUT: diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java index 0014f6b1ef1608240ccfda7858fe465914b5ded4..bd1867f55110d3bafb0c4e089f1a54c0a33d2fa1 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java @@ -5,6 +5,7 @@ import com.mindspore.common.net.HttpUtils; import com.mindspore.common.netbean.request.RequestIdBean; import com.mindspore.common.netbean.request.RequestLoginBean; import com.mindspore.common.netbean.request.RequestPageBean; +import com.mindspore.common.netbean.request.UploadBean; import com.mindspore.common.netbean.response.AccountBean; import com.mindspore.common.netbean.response.CourseDetailsBean; @@ -12,6 +13,7 @@ import com.mindspore.common.netbean.response.CourseMainMapBean; import com.mindspore.common.netbean.response.GateDetailBean; import com.mindspore.common.netbean.response.GateMainBean; import com.mindspore.common.netbean.response.LoginResBean; +import com.mindspore.common.netbean.response.UploadResBean; import java.util.List; @@ -66,22 +68,26 @@ public interface HttpClient { Observable getCourseDetailData(@Body RequestIdBean requestIdBean); @Headers("Content-Type: application/json") - @POST("api/login") + @POST("personal/login") Observable login(@Body RequestLoginBean loginBean); @Headers("Content-Type: application/json") - @POST("api/register") + @POST("personal/register") Observable register(@Body RequestLoginBean loginBean); @Headers("Content-Type: application/json") - @POST("api/logout") + @POST("personal/logout") Observable logout(); @Headers("Content-Type: application/json") - @POST("api/getPersonalDetails") + @POST("personal/getPersonalDetails") Observable getAccount(); @Headers("Content-Type: application/json") - @POST("api/updatePersonalDetails") + @POST("personal/updatePersonalDetails") Observable updateAccount(@Body AccountBean accountBean); + + @Headers("Content-Type: application/json") + @POST("upload/pictureUpload") + Observable uploadAvatar(@Body UploadBean uploadBean); } \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/BaseObserver.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/BaseObserver.java index b2e95b27d28cac319e015cd20e7b22118f2c6fc7..db1418b132ef7ed693953cdc941a0394e8b6a6f0 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/BaseObserver.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/BaseObserver.java @@ -19,6 +19,10 @@ package com.mindspore.common.net; import android.util.Log; import com.mindspore.common.exception.ExceptionHandle; +import com.mindspore.common.net.event.LoginStateChangedEvent; +import com.mindspore.common.utils.UserUtils; + +import org.greenrobot.eventbus.EventBus; import io.reactivex.observers.DefaultObserver; @@ -33,14 +37,19 @@ public abstract class BaseObserver extends DefaultObserver { @Override public void onError(Throwable throwable) { throwable.printStackTrace(); - Log.w("BaseObserver",ExceptionHandle.handleException(throwable)); - onFailed(ExceptionHandle.handleException(throwable),ExceptionHandle.errorCode); + Log.w("BaseObserver", ExceptionHandle.handleException(throwable)); + onFailed(ExceptionHandle.handleException(throwable), ExceptionHandle.errorCode); } @Override public void onComplete() { } - public void onFailed(String msg,int errorCode){} + public void onFailed(String msg, int errorCode) { + if (errorCode == ExceptionHandle.UNAUTHORIZED) { + UserUtils.cleanUserInfo(); + EventBus.getDefault().post(new LoginStateChangedEvent()); + } + } } \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/event/LoginStateChangedEvent.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/event/LoginStateChangedEvent.java similarity index 46% rename from model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/event/LoginStateChangedEvent.java rename to model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/event/LoginStateChangedEvent.java index ad69d29131ac3d5fa011bd5baa774e6d3600403c..5250de17fdb5184a27ecc2d89d633be1190a657a 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/event/LoginStateChangedEvent.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/net/event/LoginStateChangedEvent.java @@ -1,4 +1,4 @@ -package com.mindspore.himindspore.ui.me.event; +package com.mindspore.common.net.event; public class LoginStateChangedEvent { } diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/request/UploadBean.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/request/UploadBean.java new file mode 100644 index 0000000000000000000000000000000000000000..86fd99c7896cdb886a9b3d26e7c290bad7186c5d --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/request/UploadBean.java @@ -0,0 +1,17 @@ +package com.mindspore.common.netbean.request; + +public class UploadBean { + String base64Str; + + public UploadBean(String uploadImage) { + base64Str = uploadImage; + } + + public String getBase64Str() { + return base64Str; + } + + public void setBase64Str(String base64Str) { + this.base64Str = base64Str; + } +} diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/response/UploadResBean.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/response/UploadResBean.java new file mode 100644 index 0000000000000000000000000000000000000000..edb133b0f7fd8abe6b00f85ef4ea4f8e5a14553b --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/netbean/response/UploadResBean.java @@ -0,0 +1,23 @@ +package com.mindspore.common.netbean.response; + +public class UploadResBean { + String name; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + String url; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/BitmapUtils.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/BitmapUtils.java index 52d17e3f69a03e310d123ee045c34ba4c81ab041..e8f9604c391e13946239ac21de12162c01315c21 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/BitmapUtils.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/BitmapUtils.java @@ -33,6 +33,16 @@ public class BitmapUtils { private static final String TAG = "BitmapUtils"; + public static boolean isImageFile(String filePath) { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(filePath, options); + if (options.outWidth == -1) { + return false; + } + return true; + } + public static void recycleBitmap(Bitmap... bitmaps) { for (Bitmap bitmap : bitmaps) { if (bitmap != null && !bitmap.isRecycled()) { @@ -221,7 +231,7 @@ public class BitmapUtils { } ByteArrayInputStream isBm = new ByteArrayInputStream(bas.toByteArray()); return BitmapFactory.decodeStream(isBm, null, null); - }else { + } else { return null; } } @@ -342,25 +352,26 @@ public class BitmapUtils { /** * 将图片转换成Base64编码的字符串 */ - public static String imageToBase64(String path){ - if(TextUtils.isEmpty(path)){ + public static String imageToBase64(String path) { + if (TextUtils.isEmpty(path)) { return null; } InputStream is = null; byte[] data = null; String result = null; - try{ + try { is = new FileInputStream(path); //创建一个字符流大小的数组。 data = new byte[is.available()]; //写入数组 is.read(data); //用默认的编码格式进行编码 - result = Base64.encodeToString(data,Base64.NO_CLOSE); - }catch (Exception e){ + result = Base64.encodeToString(data, Base64.DEFAULT); + result = result.replaceAll("[\\s*\t\n\r]", ""); + } catch (Exception e) { e.printStackTrace(); - }finally { - if(null !=is){ + } finally { + if (null != is) { try { is.close(); } catch (IOException e) { @@ -369,20 +380,29 @@ public class BitmapUtils { } } - return result; + return getPrefix(path) + result; + } + + private static String getPrefix(String path) { + String imageSuffix = path.substring(path.lastIndexOf(".")); + if ("jpg".equals(imageSuffix)) { + imageSuffix = "jpeg"; + } + return "data:image/" + imageSuffix + ";base64,"; } /** * 将Base64编码转换为图片 + * * @param base64Str * @param path * @return true */ - public static boolean base64ToFile(String base64Str,String path) { - byte[] data = Base64.decode(base64Str,Base64.NO_WRAP); + public static boolean base64ToFile(String base64Str, String path) { + byte[] data = Base64.decode(base64Str, Base64.NO_WRAP); for (int i = 0; i < data.length; i++) { - if(data[i] < 0){ + if (data[i] < 0) { //调整异常数据 data[i] += 256; } @@ -397,12 +417,9 @@ public class BitmapUtils { } catch (FileNotFoundException e) { e.printStackTrace(); return false; - }catch (IOException e){ + } catch (IOException e) { e.printStackTrace(); return false; } } - - - } diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/UriUtils.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/UriUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..ee3d22962df502e3947bbfe84f5f6273992aee08 --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/utils/UriUtils.java @@ -0,0 +1,137 @@ +package com.mindspore.common.utils; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.MediaStore; +import android.text.TextUtils; +import android.util.Log; + +import com.mindspore.common.config.StorageUtils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class UriUtils { + private static final String TAG = UriUtils.class.getSimpleName(); + + public static String getFilePathFromUri(Uri uri, Context context) { + if (uri == null) { + return null; + } + switch (uri.getScheme()) { + case "content": + return getFileFromContentUri(uri, context); + case "file": + return uri.getPath(); + default: + return null; + } + } + + + /** + * Gets the corresponding path to a file from the given content:// URI + * + * @param contentUri The content:// URI to find the file path from + * @return the file path as a string + */ + + private static String getFileFromContentUri(Uri contentUri, Context context) { + if (contentUri == null) { + return null; + } + File file = null; + String filePath = ""; + String fileName; + String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME}; + ContentResolver contentResolver = context.getContentResolver(); + Cursor cursor = contentResolver.query(contentUri, filePathColumn, null, + null, null); + if (cursor != null) { + cursor.moveToFirst(); + int index = cursor.getColumnIndex(filePathColumn[0]); + if (index >= 0) { + filePath = cursor.getString(index); + } + fileName = cursor.getString(cursor.getColumnIndex(filePathColumn[1])); + cursor.close(); + if (!TextUtils.isEmpty(filePath)) { + file = new File(filePath); + if (!file.exists() || file.length() <= 0) { + filePath = getPathFromInputStreamUri(context, contentUri, fileName); + } + } else { + filePath = getPathFromInputStreamUri(context, contentUri, fileName); + } + } + return filePath; + } + + /** + * 用流拷贝文件一份到自己APP目录下 + * + * @param context + * @param uri + * @param fileName + * @return + */ + public static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) { + InputStream inputStream = null; + String filePath = null; + + if (uri.getAuthority() != null) { + try { + inputStream = context.getContentResolver().openInputStream(uri); + File file = createTemporalFileFrom(context, inputStream, fileName); + filePath = file.getPath(); + + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } finally { + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } + } + } + + return filePath; + } + + private static File createTemporalFileFrom(Context context, InputStream inputStream, String fileName) + throws IOException { + File targetFile = null; + + if (inputStream != null) { + int read; + byte[] buffer = new byte[8 * 1024]; + //自己定义拷贝文件路径 + targetFile = new File(StorageUtils.CRASH_PATH, fileName); + if (targetFile.exists()) { + targetFile.delete(); + } + OutputStream outputStream = new FileOutputStream(targetFile); + + while ((read = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, read); + } + outputStream.flush(); + + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return targetFile; + } +} \ No newline at end of file