再次优化图片压缩方式, 提问和反馈(先压缩再显示界面)

This commit is contained in:
kehaoyuan
2018-01-16 14:18:36 +08:00
parent c0e7b8e3ee
commit 8c3335a7bf
8 changed files with 52 additions and 44 deletions

View File

@ -28,13 +28,16 @@ public class BitmapUtils {
*/
public static boolean savePicture(String newPath, String filePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
File file = new File(newPath);
int quality = 80;
do {
try {
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
bos.flush();
@ -60,13 +63,18 @@ public class BitmapUtils {
* @return
*/
public static boolean savePicture(String newPath, String filePath, int compressSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
if (compressSize > new File(filePath).length() && bitmap.getWidth() < 960 && bitmap.getHeight() < 960) {
if (compressSize > new File(filePath).length()) {
return false;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos);
float zoom = (float) Math.sqrt(compressSize / (float) bos.toByteArray().length);

View File

@ -28,6 +28,7 @@ import android.widget.TextView;
import com.gh.base.BaseActivity;
import com.gh.base.OnListClickListener;
import com.gh.base.OnRequestCallBackListener;
import com.gh.base.fragment.WaitingDialogFragment;
import com.gh.common.constant.Config;
import com.gh.common.util.BitmapUtils;
import com.gh.common.util.CheckLoginUtils;
@ -39,9 +40,9 @@ import com.gh.common.util.PatternUtils;
import com.gh.gamecenter.entity.InstallGameEntity;
import com.gh.gamecenter.entity.SuggestionTypeEntity;
import com.gh.gamecenter.entity.UserInfoEntity;
import com.gh.base.fragment.WaitingDialogFragment;
import com.gh.gamecenter.manager.UserManager;
import com.gh.gamecenter.retrofit.JSONObjectResponse;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import com.gh.gamecenter.suggest.SuggestPicAdapter;
import com.gh.gamecenter.suggest.SuggestSelectGameAdapter;
@ -172,7 +173,25 @@ public class SuggestionActivity extends BaseActivity implements SuggestTypeAdapt
if (file.length() > 8 * 1024 * 1024) {
toast(getString(R.string.suggestion_pic_hint));
} else {
mAdapter.addFileList(picturePath);
String newPath = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg";
Observable.create((Observable.OnSubscribe<Boolean>)
subscriber -> subscriber.onNext(BitmapUtils.savePicture(newPath, picturePath)))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<Boolean>() {
@Override
public void onResponse(Boolean isSuccess) {
if (isSuccess) {
mAdapter.addFileList(newPath);
} else {
mAdapter.addFileList(picturePath);
}
}
@Override
public void onFailure(HttpException e) {
mAdapter.addFileList(picturePath);
}
});
}
}
@ -457,16 +476,8 @@ public class SuggestionActivity extends BaseActivity implements SuggestTypeAdapt
Observable.create(new Observable.OnSubscribe<JSONObject>() {
@Override
public void call(Subscriber<? super JSONObject> subscriber) {
String path;
int index = 0;
for (String s : mAdapter.getFileList()) {
path = getCacheDir() + File.separator + System.currentTimeMillis() + index + ".jpg";
if (BitmapUtils.savePicture(path, s)) {
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=suggestion", path, UserManager.getInstance().getToken()));
index++;
} else {
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=suggestion", s, UserManager.getInstance().getToken()));
}
for (String s : mAdapter.getFileList()) { // 加载图片时已经压缩
subscriber.onNext(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=suggestion", s, UserManager.getInstance().getToken()));
}
subscriber.onCompleted();
}

View File

@ -49,9 +49,7 @@ import org.json.JSONObject;
import java.io.File;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.OnClick;
@ -90,8 +88,6 @@ public class QuestionsEditFragment extends NormalFragment {
private List<String> mTagList = new ArrayList<>();
private List<String> mAllTagList = new ArrayList<>();
private Map<String, String> mMapPostImg = new HashMap<>();
private String mSearchKey;
@ -186,13 +182,7 @@ public class QuestionsEditFragment extends NormalFragment {
mPostDialog.show(getChildFragmentManager(), null);
CheckLoginUtils.checkLogin(getContext(), () -> {
List<String> fileList = mAdapter.getFileList();
List<String> imgs = new ArrayList<>();
for (String s : fileList) {
String img = mMapPostImg.get(s);
if (!TextUtils.isEmpty(img)) imgs.add(img);
}
postQuestions(imgs);
postQuestions(mAdapter.getFileList());
});
break;
case R.id.questionsedit_tips:
@ -217,8 +207,7 @@ public class QuestionsEditFragment extends NormalFragment {
Utils.log("===postImg::" + response.toString());
int statusCode = response.getInt("statusCode");
if (statusCode == HttpURLConnection.HTTP_OK) {
mAdapter.addFileList(picturePath);
mMapPostImg.put(picturePath, response.getString("icon"));
mAdapter.addFileList(response.getString("icon"));
} else if (statusCode == 403) {
toast("图片违规");
}

View File

@ -43,7 +43,7 @@ public class QuestionsEditPicAdapter extends BaseRecyclerAdapter<ViewHolder> {
viewHolder.delate.setVisibility(View.GONE);
ImageUtils.Companion.display(viewHolder.icon, R.drawable.suggest_add_pic_icon);
} else {
viewHolder.icon.setImageURI("file:///" + picList.get(position));
viewHolder.icon.setImageURI(picList.get(position));
viewHolder.delate.setVisibility(View.VISIBLE);
}

View File

@ -1,6 +1,5 @@
package com.gh.gamecenter.fragment;
import android.app.Dialog;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
@ -17,7 +16,7 @@ import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.gh.common.util.DialogUtils;
import com.gh.base.fragment.WaitingDialogFragment;
import com.gh.common.util.GetLoginDataUtils;
import com.gh.common.util.LoginUtils;
import com.gh.common.util.PatternUtils;
@ -56,7 +55,7 @@ public class LoginFragment extends NormalFragment implements LoginUtils.onCaptch
@BindView(R.id.login_phone_btn)
TextView mLoginBtn;
private Dialog mLoginDialog;
private WaitingDialogFragment mLoginDialog;
private UserViewModel mUserViewModel;
@ -201,7 +200,7 @@ public class LoginFragment extends NormalFragment implements LoginUtils.onCaptch
@Override
public void onChanged(@Nullable ApiResponse<UserInfoEntity> response) {
if (mLoginDialog != null) {
mLoginDialog.dismiss();
mLoginDialog.dismissAllowingStateLoss();
}
if (response != null && response.getData() != null && mLoginDialog != null) {
@ -218,7 +217,8 @@ public class LoginFragment extends NormalFragment implements LoginUtils.onCaptch
private void login(JSONObject content, LoginTag loginTag) {
mLoginDialog = DialogUtils.showWaitDialog(getActivity(), "登录中...");
mLoginDialog = WaitingDialogFragment.newInstance(getString(R.string.logging));
mLoginDialog.show(getChildFragmentManager(), null);
mUserViewModel.login(content, loginTag);
}

View File

@ -38,9 +38,7 @@ public class MessageFragmentAdapter extends ListAdapter {
@Override
protected <T> void provideListData(List<T> listData) {
if (mEntityList == null || mEntityList.size() == 0) {
EventBus.getDefault().post(new EBUISwitch(EB_COMMENTMARKREAD, 0)); //去除未读提示
}
EventBus.getDefault().post(new EBUISwitch(EB_COMMENTMARKREAD, 0)); //去除未读提示
mEntityList = (List<MessageEntity>) listData;
notifyDataSetChanged();
}

View File

@ -1,6 +1,5 @@
package com.gh.gamecenter.personal;
import android.app.Dialog;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
@ -15,6 +14,7 @@ import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.base.fragment.BaseFragment;
import com.gh.base.fragment.WaitingDialogFragment;
import com.gh.common.util.CheckLoginUtils;
import com.gh.common.util.DataCollectionUtils;
import com.gh.common.util.DialogUtils;
@ -120,7 +120,7 @@ public class PersonalFragment extends BaseFragment implements Observer<ApiRespon
private UserInfoEntity mUserInfoEntity;
private Dialog mLoginDialog;
private WaitingDialogFragment mLoginDialog;
private SharedPreferences sp;
@ -345,7 +345,8 @@ public class PersonalFragment extends BaseFragment implements Observer<ApiRespon
@Override
public void OnLoginData(JSONObject content, LoginTag loginTag) {
Utils.log("======获取第三方登录信息成功" + content.toString() + "===" + loginTag.name());
mLoginDialog = DialogUtils.showWaitDialog(getActivity(), "登录中...");
mLoginDialog = WaitingDialogFragment.newInstance(getString(R.string.logging));
mLoginDialog.show(getChildFragmentManager(), null);
mUserViewModel.login(content, loginTag);
}
@ -386,7 +387,7 @@ public class PersonalFragment extends BaseFragment implements Observer<ApiRespon
}
if (mLoginDialog != null) {
mLoginDialog.dismiss();
mLoginDialog.dismissAllowingStateLoss();
}
}
}