forked from wswenyue/AndroidUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageUtil.java
More file actions
109 lines (99 loc) · 2.68 KB
/
Copy pathImageUtil.java
File metadata and controls
109 lines (99 loc) · 2.68 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
109
package org.iti.eyescare.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.iti.eyescare.constants.Constants;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageUtil {
/**
* 上传图片
*
* @param filePath
* 文件路径
* @param count
* 失败重传次数
* @return
*/
public static String uploadPic(File file, int count) {
String filePathServer = "";
if (count > 0) {
try {
String result = ApacheHttpUtil.post(Constants.UPLOAD_FILE_URL,
file);
Response resp = Response.convert(result);
filePathServer = resp.getResponResult();
} catch (IOException e1) {
e1.printStackTrace();
uploadPic(file, count--);
}
}
return filePathServer;
}
/**
* 根据图片在服务器上的地址加载图片
*
* @param urlStr
* @param reqWidth
* @param reqHeight
* @return
* @throws MalformedURLException
* @throws IOException
*/
public static Bitmap decodeSampledBitmapFromUrl(String urlStr,
int reqWidth, int reqHeight) throws MalformedURLException,
IOException {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory
.decodeStream(getInputStreamFromUrl(urlStr), null, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inPurgeable = true;
options.inInputShareable = true;
return BitmapFactory.decodeStream(getInputStreamFromUrl(urlStr), null,
options);
}
/**
* 从url中得到流
*
* @param urlStr
* @return
* @throws MalformedURLException
* @throws IOException
*/
public static InputStream getInputStreamFromUrl(String urlStr)
throws MalformedURLException, IOException {
URL url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConn.getInputStream();
return inputStream;
}
/**
* 计算inSampleSize
*
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
}