Android实现上传图片功能
本文实例为大家分享了android实现上传图片功能的具体代码,供大家参考,具体内容如下
设定拍照返回的图片路径
/**
* 设定拍照返回的图片路径
* @param image 图片路径
* @param i 约定值
*/
protected void image(string image, int i) {
//创建file对象,用于存储拍照后的图片,这也是拍照成功后的照片路径
outputimage = new file(getexternalcachedir(),image);
try {
//判断文件是否存在,存在删除,不存在创建
if (outputimage.exists()){
outputimage.delete();
}
outputimage.createnewfile();
} catch (ioexception e) {
e.printstacktrace();
}
//相机拍照返回图片路径
uri photouri;
//判断当前android版本
if(build.version.sdk_int>=24){
photouri = fileprovider.geturiforfile(textactivity.this,"包名.fileprovider",outputimage);
}else {
photouri = uri.fromfile(outputimage);
}
intent intent = new intent(mediastore.action_image_capture);
intent.putextra(mediastore.extra_output, photouri);
startactivityforresult(intent, i);
}
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
调用系统相机拍照接受返回的图片路径
@override
protected void onactivityresult(int requestcode, int resultcode, intent data) {
super.onactivityresult(requestcode, resultcode, data);
if (resultcode == result_ok) {
if (requestcode == image_y) {
getimageview(binding.imagey,"0");
}
if (requestcode == image_q) {
getimageview(binding.imageq,"1");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
上传图片
/**
* 上传图片
* @param view 图片展示 view
*/
protected void getimageview(@notnull imageview view, string type) {
bitmap photo = bitmapfactory.decodefile(outputimage.getabsolutepath());
view.setimagebitmap(photo);
int direction = 0;
try {
exifinterface exif = new exifinterface(string.valueof(outputimage));
direction = integer.parseint(exif.getattribute(exifinterface.tag_orientation));
} catch (ioexception e) {
e.printstacktrace();
}
matrix matrix = new matrix();
uri uri = uri.fromfile(outputimage);
string f = uri.getpath();
bitmap b = rotatebitmap(getbitmapfromurl(f,900,1200),0);
switch (direction) {
case 1:
log.d("图片方向","顶部,左侧(水平/正常)");
b = rotatebitmap(getbitmapfromurl(f,900,1200),0);
break;
case 2:
b = rotatebitmap(getbitmapfromurl(f,900,1200),0);
log.d("图片方向","顶部,右侧(水平镜像)");
break;
case 3:
b = rotatebitmap(getbitmapfromurl(f,900,1200),180);
log.d("图片方向","底部,右侧(旋转180)");
break;
case 4:
matrix.postscale(1, -1);
b = bitmap.createbitmap(b,0,0,900,1200,matrix,true);
log.d("图片方向","底部,左侧(垂直镜像)");
break;
case 5:
matrix.postscale(-1, 1);
b = rotatebitmap(getbitmapfromurl(f,900,1200),270);
b = bitmap.createbitmap(b,0,0,900,1200,matrix,true);
log.d("图片方向","左侧,顶部(水平镜像并顺时针旋转270)");
break;
case 6:
b = rotatebitmap(getbitmapfromurl(f,900,1200),90);
log.d("图片方向","右侧,顶部(顺时针旋转90)");
break;
case 7:
matrix.postscale(-1, 1);
b = rotatebitmap(getbitmapfromurl(f,900,1200),90);
b = bitmap.createbitmap(b,0,0,900,1200,matrix,true);
log.d("图片方向","右侧,底部(水平镜像,顺时针旋转90度)");
break;
case 8:
b = rotatebitmap(getbitmapfromurl(f,900,1200),270);
log.d("图片方向","左侧,底部(顺时针旋转270)");
break;
default:
break;
}
try {
file files = new file(new uri(uri.tostring()));
savebitmap(b,files);
} catch (urisyntaxexception e) {
e.printstacktrace();
}
try {
string file = uploadimage(networkapi.getformal() + "setimage", uri.getpath(), code, userid);
textbase.fileinfo fileinfo = new textbase.fileinfo();
fileinfo.setfilepath(file);
mfileinfos.add(fileinfo);
model.load(file,type);
} catch (ioexception | jsonexception e) {
e.printstacktrace();
}
}
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
/**
* 上传图片
* @param url 上传接口路径
* @param imagepath 图片路径
* @param code 唯一标识
* @return 服务器图片的路径
* @throws ioexception
* @throws jsonexception
*/
public static string uploadimage(string url, string imagepath, string code, string userid) throws ioexception, jsonexception {
okhttpclient okhttpclient = new okhttpclient();
log.d("imagepath", imagepath);
file file = new file(imagepath);
requestbody image = requestbody.create(mediatype.parse("image/jpg"), file);
requestbody requestbody = new multipartbody.builder()
.settype(multipartbody.form)
.addformdatapart("file", imagepath, image)
.addformdatapart("fileoid", code)
.addformdatapart("userid", userid)
.build();
request request = new request.builder()
.url(url)
.addheader("authorization",令牌)
.post(requestbody)
.build();
response response = okhttpclient.newcall(request).execute();
jsonobject jsonobject = new jsonobject(response.body().string());
return jsonobject.optstring("msg");
}
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
其他工具类
/**
* 压缩图片的方法
* @param url
* @param width
* @param height
* @return
*/
public static bitmap getbitmapfromurl(string url, double width, double height) {
bitmapfactory.options options = new bitmapfactory.options();
options.injustdecodebounds = true; // 设置了此属性一定要记得将值设置为false
bitmap bitmap = bitmapfactory.decodefile(url);
// 防止oom发生
options.injustdecodebounds = false;
int mwidth = bitmap.getwidth();
int mheight = bitmap.getheight();
matrix matrix = new matrix();
float scalewidth = 1;
float scaleheight = 1;
// 按照固定宽高进行缩放
if(mwidth <= mheight) {
scalewidth = (float) (width/mwidth);
scaleheight = (float) (height/mheight);
} else {
scalewidth = (float) (height/mwidth);
scaleheight = (float) (width/mheight);
}
// 按照固定大小对图片进行缩放
matrix.postscale(scalewidth, scaleheight);
bitmap newbitmap = bitmap.createbitmap(bitmap, 0, 0, mwidth, mheight, matrix, true);
// 用完了记得回收
bitmap.recycle();
return newbitmap;
}
/**
* android保存bitmap到文件
* @param bitmap
* @param file
* @return
*/
public static boolean savebitmap(bitmap bitmap, file file) {
if (bitmap == null)
return false;
fileoutputstream fos = null;
try {
fos = new fileoutputstream(file);
bitmap.compress(bitmap.compressformat.jpeg, 100, fos);
fos.flush();
return true;
} catch (exception e) {
e.printstacktrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
return false;
}
/**
* 旋转图片
* @param bitmap 图片
* @param rotate 角度
* @return
*/
public static bitmap rotatebitmap(bitmap bitmap, int rotate) {
if (bitmap == null)
return null;
int w = bitmap.getwidth();
int h = bitmap.getheight();
matrix mtx = new matrix();
mtx.postrotate(rotate);
return bitmap.createbitmap(bitmap, 0, 0, w, h, mtx, true);
}
上一篇: 肉泡在水里解冻正确吗