一、相关知识
①Android权限申请
②网络访问框架OKHttp
③内存溢出问题:图片压缩
④Android 系统7.0以上调用系统相机无效
⑤有关图片上传过程中遇到的内存溢出问题
二、效果展示

二、代码
①HTML
1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:orientation="vertical" 5 android:background="@color/white" 6 > 7 <android.support.v7.widget.RecyclerView 8 android:id="@+id/rvPic" 9 android:layout_width="wrap_content" 10 android:layout_height="match_parent" 11 android:layout_gravity="center_horizontal"> 12 13 </android.support.v7.widget.RecyclerView> 14 15 <TextView 16 android:id="@+id/tvNum" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="0/8" 20 android:textColor="#666666" 21 android:layout_gravity="right|bottom" 22 android:paddingRight="@dimen/dp_10"/> 23 24 25 </LinearLayout> 26 <Button 27 28 android:id="@+id/btn_Enter" 29 android:layout_width="match_parent" 30 android:layout_height="@dimen/dp_45" 31 android:layout_alignParentBottom="true" 32 android:background="@drawable/selecter_button" 33 android:text="确认上传" 34 android:textColor="@color/inButtonText" 35 android:textSize="@dimen/dp_18" />
②Java代码
<基本功能>
实体类
1 public class LoadFileVo {
2
3 File file;
4
5 int pg; //图片下方的进度条
6
7 boolean isUpload = false; //标识该文件是否上传
8
9 Bitmap bitmap;
10
11 public Bitmap getBitmap() {
12 return bitmap;
13 }
14
15 public void setBitmap(Bitmap bitmap) {
16 this.bitmap = bitmap;
17 }
18
19 public boolean isUpload() {
20 return isUpload;
21 }
22
23 public void setUpload(boolean upload) {
24 isUpload = upload;
25 }
26
27 public LoadFileVo() {
28 }
29
30 public LoadFileVo(File file, int pg) {
31 this.file = file;
32 this.pg = pg;
33 }
34
35 public LoadFileVo(File file, boolean isUpload, int pg,Bitmap bitmap) {
36 this.file = file;
37 this.pg = pg;
38 this.isUpload = isUpload;
39 this.bitmap = bitmap;
40 }
41
42 public File getFile() {
43 return file;
44 }
45
46 public void setFile(File file) {
47 this.file = file;
48 }
49
50 public int getPg() {
51 return pg;
52 }
53
54 public void setPg(int pg) {
55 this.pg = pg;
56 }
57 }
适配器
1 /*
2 *Create By 小群子 2018/12/10
3 */
4
5 public class LoadPicAdapter extends RecyclerView.Adapter<LoadPicAdapter.MyViewHolder> {
6
7 Context context;
8 List<LoadFileVo> fileList = null;
9 View view;
10 int picNum = 8;//列表的图片个数最大值
11
12 public LoadPicAdapter(Context context, List<LoadFileVo> fileList) {
13 this.context = context;
14 this.fileList = fileList;
15 }
16
17 public LoadPicAdapter(Context context, List<LoadFileVo> fileList, int picNum) {
18 this.context = context;
19 this.fileList = fileList;
20 this.picNum = picNum;
21 }
22
23 public interface OnItemClickListener {
24 void click(View view, int positon);
25
26 void del(View view);
27 }
28
29 OnItemClickListener listener;
30
31 public void setListener(OnItemClickListener listener) {
32 this.listener = listener;
33 }
34
35 @Override
36 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
37
38 view = LayoutInflater.from(context).inflate(R.layout.load_item_pic, parent, false);
39 return new MyViewHolder(view);
40 }

这篇博客介绍了Android使用OKHttp上传图片到服务器的完整流程,包括权限申请、图片压缩防止内存溢出、7.0以上系统相机兼容问题以及核心代码实现。并提供了相关技术大纲和面试题集资源。
最低0.47元/天 解锁文章
676





