Android 上传图片到服务器 okhttp一

一、相关知识

①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         re
  • 4
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值