-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadImage.java
More file actions
108 lines (89 loc) · 3.3 KB
/
Copy pathLoadImage.java
File metadata and controls
108 lines (89 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.example.kongalong.ximalaya_mvp.imageLoad;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**网络加载
* Created by kongalong on 2016/11/8.
*/
public class LoadImage {
private Context mContext;
public LoadImage(Context context) {
mContext = context;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public Bitmap laodDataByHttp(String path, Caches diskCache, int width, int height){
Log.d("flag", "beginLoadBitmap: 从网络获取");
InputStream bis = null;
ByteArrayOutputStream baos = null;
HttpURLConnection conn = null;
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(Constants.TIME_OUT);
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
bis = new BufferedInputStream(conn.getInputStream());
baos = new ByteArrayOutputStream();
int len = 0;
byte[] buf = new byte[Constants.BYTE_BUFFER_SIZE];
while((len = bis.read(buf))!=-1){
baos.write(buf,0,len);
}
byte[] data = baos.toByteArray();
Bitmap bitmap = CompressBitmap.compressFromBytes(data, width, height);
//保存到caches缓存);
if(diskCache.isCreateDiskCache){
diskCache.saveDataToDisk(path, data);
}
diskCache.saveDataToMem(path,bitmap);
//返回压缩后的图片
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
conn.disconnect();
Utils.closeOs(baos);
Utils.closeIs(bis);
}
return null;
}
/* public static Bitmap laodDataByHttp(String path, Caches diskCache, int width, int height){
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
if(connection.getResponseCode()==200){
is = connection.getInputStream();
baos = new ByteArrayOutputStream();
int len = 0;
byte[] buf = new byte[1024*8];
while((len = is.read(buf))!= -1){
baos.write(buf,0,len);
}
byte[] bytes = baos.toByteArray();
return BitmapFactory.decodeByteArray(bytes,0,bytes.length);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
Utils.closeIs(is);
Utils.closeOs(baos);
}
return null;
}*/
}