总结下获取本地图片并上传:
准备工作:需要用到的函数 AsyncHttpClient, 是一个框架提供的库 可以异步传输,使用时需下载android-async-http-1.4.4.jar包导入到项目中,下载地址:Jar下载地址 导入项目即可
001.获取SD卡目录下的图片路径:
private String getImg() {
String filePath=Environment.getExternalStorageDirectory()+"/6-i.jpg";//sd卡根目录下的图片:6-i.jpg
Log.i("Info", filePath.length()+"");
return filePath;
}
002.将获取的的图片转为Bitmap格式:
- private Bitmap getBitmap(String filePath)
- {
- Bitmap bitmap = null;
- try {
- File file = new File(filePath);
- if(file.exists()) {
- bitmap = BitmapFactory.decodeFile(pathString);
- }
- } catch (Exception e) {
- }
- return bitmap;
- }
003.将Bitmap格式的图片上传至服务器:
public void submitService(final Context cont,Bitmap photodata) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式,100:压缩率,baos:字节流
photodata.compress(Bitmap.CompressFormat.PNG, 100, baos);
baos.close();
byte[] buffer = baos.toByteArray();
System.out.println("图片的大小:"+buffer.length);
//将图片的字节流数据加密成base64字符输出(为了和服务器那边对应我改为16进制编码)
//ssString photo = Base64.encodeToString(buffer, 0, buffer.length,Base64.DEFAULT);
//将bitmap格式的图片处理为16进制
String photoStr = byte2hex(buffer);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式,100:压缩率,baos:字节流
photodata.compress(Bitmap.CompressFormat.PNG, 100, baos);
baos.close();
byte[] buffer = baos.toByteArray();
System.out.println("图片的大小:"+buffer.length);
//将图片的字节流数据加密成base64字符输出(为了和服务器那边对应我改为16进制编码)
//ssString photo = Base64.encodeToString(buffer, 0, buffer.length,Base64.DEFAULT);
//将bitmap格式的图片处理为16进制
String photoStr = byte2hex(buffer);
//photo=URLEncoder.encode(photo,"UTF-8");
RequestParams params = new RequestParams();
//此处的参数和服务器那边保持一致
params.put("updir", photoStr);
params.put("updir", photoStr);
//服务器地址
String url = "http://www.xuanqiqi.com/api/wap/?ac=share&do=shareinfo";
//进行上传
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, String content){
Log.i("Info", statusCode+"");//打印信息content表示服务器返回内容
}
@Override
public void onFailure(Throwable e, String data){
Toast.makeText(cont, "图片上传失败!", 0).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
String url = "http://www.xuanqiqi.com/api/wap/?ac=share&do=shareinfo";
//进行上传
AsyncHttpClient client = new AsyncHttpClient();
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, String content){
Log.i("Info", statusCode+"");//打印信息content表示服务器返回内容
}
@Override
public void onFailure(Throwable e, String data){
Toast.makeText(cont, "图片上传失败!", 0).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
//获取16进制的图片转码
private String byte2hex(byte[] buffer) {
StringBuffer sb = new StringBuffer();
String stmp = "";
for (int n = 0; n < buffer.length; n++) {
stmp = Integer.toHexString(buffer[n] & 0XFF);
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}
}
return sb.toString();
}
StringBuffer sb = new StringBuffer();
String stmp = "";
for (int n = 0; n < buffer.length; n++) {
stmp = Integer.toHexString(buffer[n] & 0XFF);
if (stmp.length() == 1) {
sb.append("0" + stmp);
} else {
sb.append(stmp);
}
}
return sb.toString();
}