本人初学者,写东西的最根本目的也是加深印象, 先来效果图:
做这个小Demo,主要是有两步需要实现:
第一:是点击ImageView控件,弹出对话框,选择相册或者拍照后的图片设置到imageview上
第二:是把该图片上传到服务器,(在网上找了好多例子,大都是设置请求头信息什么的),这里我用的是根据Base64 上传。需要在Android 和Servlet段导入jar包(android-async-http-1.4.3.jar) ; Baser基本思想是在Android段把图片解析为String类型的字节数组,发送到服务器端,解密
说一下具体方法的实现:
第一步:利用onResultActivity拿到相册图片的输入流
private byte[] readStream(InputStream openInputStream) {
byte[] buffer = new byte[1024];
int len = -1;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
while ((len = openInputStream.read(buffer)) != -1) {// 不等于-1 继续读取
out.write(buffer, 0, len);
}
byte[] data = out.toByteArray();
out.close();
openInputStream.close();
return data;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private Bitmap getPicFromBytes(byte[] mContent2, Object object) {
if (mContent2 != null) {
if (object != null) {
return BitmapFactory.decodeByteArray(mContent2, 0,
mContent2.length);
} else {
return BitmapFactory.decodeByteArray(mContent2, 0,
mContent2.length);
}
}
return myBitMap;
}
String proj[]={MediaStore.Images.Media.DATA};
Cursor cursor=managedQuery(uri, proj, null, null, null);//uri就是上述的uri
String ss= uri.getEncodedPath();
System.out.println("ssssssssss"+ss);
int coulun_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String realPath=cursor.getString(coulun_index);
System.out.println("realPath"+realPath);
Bitmap bit = BitmapFactory.decodeFile(realPath);
)
接下俩就开始根绝Base64上传(很简单)<pre name="code" class="html">public static void reg(final Context cont,final Bitmap bit) throws IOException{
//这边,我们已经拿到Bitmap. 要根据Base 64.
ByteArrayOutputStream baso=new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.JPEG, 90, baso);
// baso.close();
byte aa[]=baso.toByteArray();
System.out.println("图片的大小为:"+aa.length);
String photo=Base64.encodeToString(aa, 0, aa.length, Base64.DEFAULT);
// String photo=Base64.encodeToString(aa, 0);
RequestParams parms=new RequestParams();
parms.put("photo", photo); parms.put("name", "wjjer");
String url="http://10.203.1.51:8080/Test/User";
AsyncHttpClient client=new AsyncHttpClient();
client.post(url, parms,new AsyncHttpResponseHandler(){};
: });
} 看这个文章的上述代码都肯定明白;至此我们已经完成了上传服务器的工作;接下来是在Servlet 接收;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String photo=request.getParameter("photo");
String name=request.getParameter("name");
System.out.println(photo);
//对发送来的Base64数据进行解码。生成字节数组
byte []photos=new BASE64Decoder().decodeBuffer(photo);
for (int i=0;i<photos.length;i++){
if(photos[i]<0){
photos[i]+=256;
}
}
//F
File file=new File("d:","decode.png");
File filename=new File("d:\\name.txt");
if(!file.exists()){file.createNewFile();}if(!filename.exists()){filename.createNewFile();}
FileOutputStream out=new FileOutputStream(file);
FileOutputStream out1=new FileOutputStream(filename);
out.write(photos);out.flush();out.close();
}
};