From 16de588c30df1da65fc159665693692f3dc73bb5 Mon Sep 17 00:00:00 2001 From: kehaoyuan Date: Wed, 11 Dec 2019 15:11:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=96=B0=E6=95=B4=E7=90=86=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=89=AA=E8=A3=81=E4=BB=A5=E4=BE=BF=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E8=A7=86=E9=A2=91=E5=B0=81=E9=9D=A2=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 9 +- .../com/gh/common/util/EntranceUtils.java | 1 + .../gh/common/view/CropImageBorderView.java | 12 +- .../com/gh/common/view/CropImageCustom.java | 21 +-- .../com/gh/common/view/CropImageZoomView.java | 22 ++- .../com/gh/gamecenter/CropImageActivity.java | 162 +++------------- .../fragment/user/SelectPortraitFragment.java | 10 +- .../user/UserPortraitCropImageActivity.java | 177 ++++++++++++++++++ 8 files changed, 248 insertions(+), 166 deletions(-) create mode 100644 app/src/main/java/com/halo/assistant/fragment/user/UserPortraitCropImageActivity.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2df9dd8e3b..ede21c338b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,8 +28,9 @@ - + @@ -451,6 +452,10 @@ android:name="com.gh.gamecenter.game.upload.GameSubmissionActivity" android:screenOrientation="portrait" /> + + reference; - private SharedPreferences sp; - - @Override - protected void handleMessage(Message msg) { - switch (msg.what) { - case 0: - toast("上传成功"); - break; - case 1: - toast("上传失败"); - break; - case 2: - toast("修改太频繁,请稍后再试"); - break; - case 3: - toast("图片违规"); - break; - } - } - @NonNull - public static Intent getIntent(Context context, String picturePath, String entrance) { + public static Intent getIntent(Context context, String picturePath, float cropRatio, String entrance) { Intent intent = new Intent(context, CropImageActivity.class); intent.putExtra(EntranceUtils.KEY_PATH, picturePath); intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance); + intent.putExtra(EntranceUtils.KEY_IMAGE_CROP_RATIO, cropRatio); return intent; } @@ -84,108 +50,28 @@ public class CropImageActivity extends ToolBarActivity { setNavigationTitle(getString(R.string.title_crop_image)); setToolbarMenu(R.menu.menu_positive); - sp = PreferenceManager.getDefaultSharedPreferences(this); + float ratio = getIntent().getFloatExtra(EntranceUtils.KEY_IMAGE_CROP_RATIO, 1F); + mCropImageCustom.setCropRatio(ratio); } @Override public boolean onMenuItemClick(MenuItem item) { if (item.getItemId() == R.id.layout_menu_positive) { - final WaitingDialogFragment postDialog = WaitingDialogFragment.newInstance(getString(R.string.post_img)); - postDialog.show(getSupportFragmentManager(), null); - final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg"; - Observable.create((ObservableOnSubscribe) emitter -> { - boolean isSuccess = mCropimageCustom.savePicture(path); - if (isSuccess) { - UploadImageUtils.INSTANCE.uploadImage(UploadImageUtils.UploadType.icon, path, new UploadImageUtils.OnUploadImageListener() { - @Override - public void onSuccess(@NotNull String imageUrl) { - emitter.onNext(imageUrl); - emitter.onComplete(); - } + Intent data = new Intent(); + Bitmap clip = mCropImageCustom.clip(); + String clipPath = getCacheDir().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg"; + try { + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(clipPath))); + clip.compress(Bitmap.CompressFormat.WEBP, 100, bos); + bos.flush(); + bos.close(); + } catch (Exception e) { + e.printStackTrace(); + } - @Override - public void onError(@Nullable Throwable e) { - if (e != null) { - emitter.onError(e); - } else { - emitter.onError(new IllegalStateException("upload image error")); - } - } - - @Override - public void onProgress(long total, long progress) { - int percent = (int) (100 * (progress / (float) total)); - if (percent >= 100) percent = 99; - if (postDialog != null) { - postDialog.uploadWaitingHint("图片上传中 " + percent + "%"); - } - } - }); - } - }).subscribeOn(Schedulers.io()) - .observeOn(Schedulers.io()) - .subscribe(new Response() { - @Override - public void onResponse(String url) { - try { - if (postDialog != null) postDialog.dismissAllowingStateLoss(); - mBaseHandler.sendEmptyMessage(0); - - String iconCount = sp.getString("updateIconCount", null); - - long l = System.currentTimeMillis(); - SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA); - String time = format.format(new Date(l)); - - JSONObject jsonObject = new JSONObject(); - jsonObject.put("time", time); - - if (TextUtils.isEmpty(iconCount)) { - jsonObject.put("count", 1); - } else { - JSONObject json = new JSONObject(iconCount); - String lastTime = json.getString("time"); - if (lastTime.equals(time)) { - jsonObject.put("count", json.getInt("count") + 1); - } else { - jsonObject.put("count", 1); - } - } - - sp.edit().putString("updateIconCount", jsonObject.toString()).apply(); - - Intent data = new Intent(); - data.putExtra(EntranceUtils.KEY_URL, url); - setResult(RESULT_OK, data); - finish(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @Override - public void onFailure(HttpException e) { - if (postDialog != null) postDialog.dismissAllowingStateLoss(); - try { - if (e != null && e.code() == HttpURLConnection.HTTP_FORBIDDEN && e.response().errorBody() != null) { - JSONObject object = new JSONObject(e.response().errorBody().string()); - String detail = object.getString("detail"); - if ("too frequent".equals(detail)) { - mBaseHandler.sendEmptyMessage(2); - } else if ("INVALID PICTURE".equals(detail)) { - mBaseHandler.sendEmptyMessage(3); - } else { - mBaseHandler.sendEmptyMessage(1); - } - } else { - mBaseHandler.sendEmptyMessage(1); - } - } catch (Exception e1) { - e1.printStackTrace(); - mBaseHandler.sendEmptyMessage(1); - } - } - }); + data.putExtra(RESULT_CLIP_PATH, clipPath); + setResult(RESULT_OK, data); + finish(); } return super.onMenuItemClick(item); } @@ -202,7 +88,7 @@ public class CropImageActivity extends ToolBarActivity { public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus && (reference == null || reference.get() == null)) { - ImageView imageView = mCropimageCustom.getCropImageZoomView(); + ImageView imageView = mCropImageCustom.getCropImageZoomView(); Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra(EntranceUtils.KEY_PATH), imageView.getWidth(), imageView.getHeight()); if (bitmap != null) { diff --git a/app/src/main/java/com/halo/assistant/fragment/user/SelectPortraitFragment.java b/app/src/main/java/com/halo/assistant/fragment/user/SelectPortraitFragment.java index bd0d400387..bf53d60787 100644 --- a/app/src/main/java/com/halo/assistant/fragment/user/SelectPortraitFragment.java +++ b/app/src/main/java/com/halo/assistant/fragment/user/SelectPortraitFragment.java @@ -12,6 +12,10 @@ import android.provider.MediaStore; import android.text.TextUtils; import android.view.View; +import androidx.annotation.Nullable; +import androidx.lifecycle.Observer; +import androidx.lifecycle.ViewModelProviders; + import com.gh.common.util.DialogUtils; import com.gh.common.util.EntranceUtils; import com.gh.common.util.PermissionHelper; @@ -32,9 +36,6 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; -import androidx.annotation.Nullable; -import androidx.lifecycle.Observer; -import androidx.lifecycle.ViewModelProviders; import butterknife.OnClick; /** @@ -89,7 +90,8 @@ public class SelectPortraitFragment extends NormalFragment { Utils.log("picturePath = " + picturePath); // 上传头像 - Intent intent = CropImageActivity.getIntent(getContext(), picturePath, "我的光环(选择头像)"); + Intent intent = CropImageActivity.getIntent(getContext(), + picturePath, 4/3F, "我的光环(选择头像)"); startActivityForResult(intent, REQUEST_CROP_ICON); break; case REQUEST_CROP_ICON: diff --git a/app/src/main/java/com/halo/assistant/fragment/user/UserPortraitCropImageActivity.java b/app/src/main/java/com/halo/assistant/fragment/user/UserPortraitCropImageActivity.java new file mode 100644 index 0000000000..2ea869c1a0 --- /dev/null +++ b/app/src/main/java/com/halo/assistant/fragment/user/UserPortraitCropImageActivity.java @@ -0,0 +1,177 @@ +package com.halo.assistant.fragment.user; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.Message; +import android.preference.PreferenceManager; +import android.text.TextUtils; +import android.view.MenuItem; + +import androidx.annotation.NonNull; + +import com.gh.base.fragment.WaitingDialogFragment; +import com.gh.common.util.EntranceUtils; +import com.gh.common.util.UploadImageUtils; +import com.gh.gamecenter.CropImageActivity; +import com.gh.gamecenter.R; +import com.gh.gamecenter.retrofit.Response; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.json.JSONObject; + +import java.io.File; +import java.net.HttpURLConnection; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import io.reactivex.Observable; +import io.reactivex.ObservableOnSubscribe; +import io.reactivex.schedulers.Schedulers; +import retrofit2.HttpException; + +public class UserPortraitCropImageActivity extends CropImageActivity { + + private SharedPreferences sp; + + @Override + protected void handleMessage(Message msg) { + switch (msg.what) { + case 0: + toast("上传成功"); + break; + case 1: + toast("上传失败"); + break; + case 2: + toast("修改太频繁,请稍后再试"); + break; + case 3: + toast("图片违规"); + break; + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + sp = PreferenceManager.getDefaultSharedPreferences(this); + } + + @Override + public boolean onMenuItemClick(MenuItem item) { + if (item.getItemId() == R.id.layout_menu_positive) { + final WaitingDialogFragment postDialog = WaitingDialogFragment.newInstance(getString(R.string.post_img)); + postDialog.show(getSupportFragmentManager(), null); + final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg"; + Observable.create((ObservableOnSubscribe) emitter -> { + boolean isSuccess = mCropImageCustom.savePicture(path); + if (isSuccess) { + UploadImageUtils.INSTANCE.uploadImage(UploadImageUtils.UploadType.icon, path, new UploadImageUtils.OnUploadImageListener() { + @Override + public void onSuccess(@NotNull String imageUrl) { + emitter.onNext(imageUrl); + emitter.onComplete(); + } + + @Override + public void onError(@Nullable Throwable e) { + if (e != null) { + emitter.onError(e); + } else { + emitter.onError(new IllegalStateException("upload image error")); + } + } + + @Override + public void onProgress(long total, long progress) { + int percent = (int) (100 * (progress / (float) total)); + if (percent >= 100) percent = 99; + if (postDialog != null) { + postDialog.uploadWaitingHint("图片上传中 " + percent + "%"); + } + } + }); + } + }).subscribeOn(Schedulers.io()) + .observeOn(Schedulers.io()) + .subscribe(new Response() { + @Override + public void onResponse(String url) { + try { + if (postDialog != null) postDialog.dismissAllowingStateLoss(); + mBaseHandler.sendEmptyMessage(0); + + String iconCount = sp.getString("updateIconCount", null); + + long l = System.currentTimeMillis(); + SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA); + String time = format.format(new Date(l)); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("time", time); + + if (TextUtils.isEmpty(iconCount)) { + jsonObject.put("count", 1); + } else { + JSONObject json = new JSONObject(iconCount); + String lastTime = json.getString("time"); + if (lastTime.equals(time)) { + jsonObject.put("count", json.getInt("count") + 1); + } else { + jsonObject.put("count", 1); + } + } + + sp.edit().putString("updateIconCount", jsonObject.toString()).apply(); + + Intent data = new Intent(); + data.putExtra(EntranceUtils.KEY_URL, url); + setResult(RESULT_OK, data); + finish(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void onFailure(HttpException e) { + if (postDialog != null) postDialog.dismissAllowingStateLoss(); + try { + if (e != null && e.code() == HttpURLConnection.HTTP_FORBIDDEN && e.response().errorBody() != null) { + JSONObject object = new JSONObject(e.response().errorBody().string()); + String detail = object.getString("detail"); + if ("too frequent".equals(detail)) { + mBaseHandler.sendEmptyMessage(2); + } else if ("INVALID PICTURE".equals(detail)) { + mBaseHandler.sendEmptyMessage(3); + } else { + mBaseHandler.sendEmptyMessage(1); + } + } else { + mBaseHandler.sendEmptyMessage(1); + } + } catch (Exception e1) { + e1.printStackTrace(); + mBaseHandler.sendEmptyMessage(1); + } + } + }); + + return false; + } + return super.onMenuItemClick(item); + } + + @NonNull + public static Intent getIntent(Context context, String picturePath, float cropRatio, String entrance) { + Intent intent = new Intent(context, UserPortraitCropImageActivity.class); + intent.putExtra(EntranceUtils.KEY_PATH, picturePath); + intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance); + intent.putExtra(EntranceUtils.KEY_IMAGE_CROP_RATIO, cropRatio); + return intent; + } +}