1.拍照头像需要经过缩放处理再压缩体积

2.完善头像显示
0515
panqihua 5 years ago
parent 057cd6309e
commit 5ac2e79932
  1. 17
      app/src/main/java/com/community/pocket/data/model/Forum.java
  2. 9
      app/src/main/java/com/community/pocket/ui/main/ui/forum/ForumPost.java
  3. 9
      app/src/main/java/com/community/pocket/ui/main/ui/forum/data/ForumDataActivity.java
  4. 25
      app/src/main/java/com/community/pocket/ui/main/ui/info/InfoFragment.java
  5. 1
      app/src/main/res/values-en-rUS/strings.xml
  6. 1
      app/src/main/res/values-zh-rCN/strings.xml
  7. 4
      app/src/main/res/values/integers.xml
  8. 1
      app/src/main/res/values/strings.xml

@ -19,13 +19,16 @@ public class Forum {
//审核状态
private ForumStatus status;
//其他信息
//悬赏贴信息
private Active activeDto;
//投诉贴信息
private Complain complainDto;
//结算贴信息
private Score score;
//发帖人详情信息
private MyInfo info;
public Active getActiveDto() {
return activeDto;
}
@ -113,4 +116,12 @@ public class Forum {
public void setScore(Score score) {
this.score = score;
}
public MyInfo getInfo() {
return info;
}
public void setInfo(MyInfo info) {
this.info = info;
}
}

@ -1,11 +1,13 @@
package com.community.pocket.ui.main.ui.forum;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -13,8 +15,10 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SearchView;
import androidx.core.widget.NestedScrollView;
import androidx.lifecycle.MutableLiveData;
import com.community.pocket.R;
import com.community.pocket.data.main.info.InfoRequest;
import com.community.pocket.data.model.Forum;
import com.community.pocket.data.model.Page;
import com.community.pocket.ui.BaseFragment;
@ -75,6 +79,11 @@ public abstract class ForumPost extends BaseFragment {
author.setVisibility(showAuthor());
author.setText(getString(R.string.poster, forum.getUsername()));
ImageView headimg = childView.findViewById(R.id.headimg);
MutableLiveData<Bitmap> liveData = new MutableLiveData<>();
InfoRequest.getInstance().getImg(liveData, forum.getInfo().getHeadImg());
liveData.observe(getViewLifecycleOwner(), headimg::setImageBitmap);
TextView postReply = childView.findViewById(R.id.post_reply);
postReply.setText(getString(R.string.post_reply, forum.getReply()));
TextView time = childView.findViewById(R.id.post_time);

@ -2,6 +2,7 @@ package com.community.pocket.ui.main.ui.forum.data;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
@ -11,6 +12,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
@ -19,9 +21,11 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProvider;
import com.community.pocket.R;
import com.community.pocket.data.main.info.InfoRequest;
import com.community.pocket.data.model.Active;
import com.community.pocket.data.model.Complain;
import com.community.pocket.data.model.Forum;
@ -262,5 +266,10 @@ public class ForumDataActivity extends BaseActivity {
tower.setText(getString(R.string.tower, forumContent.getTower()));
TextView content = view.findViewById(R.id.content);
content.setText(forumContent.getContent());
ImageView headimg = view.findViewById(R.id.headimg);
MutableLiveData<Bitmap> liveData = new MutableLiveData<>();
InfoRequest.getInstance().getImg(liveData, forumContent.getInfo().getHeadImg());
liveData.observe(this, headimg::setImageBitmap);
}
}

@ -362,32 +362,40 @@ public class InfoFragment extends BaseFragment {
if (picFile.createNewFile()) {
Bitmap bitmap;
Bitmap srcbitmap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext().getContentResolver(), uri));
srcbitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(requireContext().getContentResolver(), uri));
} else {
bitmap = MediaStore.Images.Media.getBitmap(requireContext().getContentResolver(), uri);
srcbitmap = MediaStore.Images.Media.getBitmap(requireContext().getContentResolver(), uri);
}
Context context = requireContext();
TextView textView = new TextView(getContext());
TextView textView = new TextView(context);
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
textView.setText(R.string.compress_img);
textView.setText(R.string.scale_img);
AlertDialog uploadDialog = new AlertDialog.Builder(context)
.setView(textView).setCancelable(false).show();
//缩放图片
int size = getResources().getInteger(R.integer.photo_size);
Bitmap scalebitmap = Bitmap.createScaledBitmap(srcbitmap, size, size, true);
compressHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
if (msg.what == Response.Result.OK.ordinal()) {
Log.i(InfoFragment.class.getName(), "位图大小:" + bitmap.getByteCount() / 1024 + "KB" + ",压缩大小" + picFile.length() / 1024 + "KB");
Log.i(InfoFragment.class.getName(), "图大小:" + srcbitmap.getByteCount() / 1024 / 1024F + "MB,缩放大小:" + scalebitmap.getByteCount() / 1024 / 1024F + "MB,压缩大小:" + picFile.length() / 1024 / 1024F + "MB");
uploadImgHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
uploadDialog.dismiss();
if (msg.what == Response.Result.OK.ordinal()) {
headimg.setImageBitmap(bitmap);
headimg.setImageBitmap(scalebitmap);
}
}
};
@ -400,9 +408,10 @@ public class InfoFragment extends BaseFragment {
}
};
textView.setText(R.string.compress_img);
new Thread(() -> {
try {
bitmap.compress(Bitmap.CompressFormat.PNG, getResources().getInteger(R.integer.photo_quality), new FileOutputStream(
scalebitmap.compress(Bitmap.CompressFormat.PNG, getResources().getInteger(R.integer.photo_quality), new FileOutputStream(
picFile));
compressHandler.sendEmptyMessage(Response.Result.OK.ordinal());
} catch (FileNotFoundException e) {

@ -237,4 +237,5 @@
<string name="set_headimg">click to set headimg</string>
<string name="upload_img">upload img....</string>
<string name="compress_img">compress img…</string>
<string name="scale_img">scale image...</string>
</resources>

@ -237,4 +237,5 @@
<string name="set_headimg">点击上方设置头像</string>
<string name="upload_img">上传头像中。。。。</string>
<string name="compress_img">压缩图片中。。。</string>
<string name="scale_img">缩放图片。。。</string>
</resources>

@ -2,6 +2,6 @@
<resources>
<integer name="post_content">200</integer>
<integer name="post_content_maxLines">10</integer>
<integer name="photo_size">100</integer>
<integer name="photo_quality">50</integer>
<integer name="photo_size">1024</integer>
<integer name="photo_quality">100</integer>
</resources>

@ -238,6 +238,7 @@
<string name="set_headimg">click to set headimg</string>
<string name="upload_img">upload img....</string>
<string name="compress_img">compress img…</string>
<string name="scale_img">scale image...</string>
<!-- Strings used for fragments for navigation -->
<!-- Strings used for fragments for navigation -->

Loading…
Cancel
Save