android webservice 上传下载图片

项目中 有 需求上传和下载图片,和后台接口是webservice写的,没用框架,请指正

思路
      上传:需要先将图片旋转到正确的角度,然后压缩大小,最后上传,上传图片的时候将图片转换成 base64编码的String长字符串,同时上传图片名称字段方便后台保存(主要是上传图片格式)
      下载:下载某篇含该图片的的文章,xml节点为该图片的网络地址,如http:asd/asdfsd.jpg,采用 多线程下载图片

写的不具体,只是 专为 图片部分所做

上传部分:
(本文中图片包含在Diary类中上传)
	public String WriteDiary(Account account , Diary diary,String url) {
		try {
			SoapObject rpc = new SoapObject("", "writeDiary");
			//图片  图片没有修改,则不用上传图片
			if(!diary.getOldImagePath() .equals(diary.getImagePath())){
				rpc.addProperty("image", ImageTool.getInstance().getBase64StringWithPath(diary.getImagePath()));
				rpc.addProperty("imageName", diary.getImagePath());
			}else{
				rpc.addProperty("image", "");
				rpc.addProperty("imageName", "");
			}
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			(new MarshalBase64()).register(envelope);
			HttpTransportSE ht = new HttpTransportSE(url,
					Config.time_out_http_connect);
			ht.debug = true;
			ht.call(null, envelope);
			Object result = (Object) envelope.getResponse();
			return result.toString();
		} catch (Exception e) {
			// e.printStackTrace();
			System.out.print(e.toString());
		}
		return null;
	}
其中 最重要的就是 
ImageTool.getInstance().getBase64StringWithPath(diary.getImagePath())
我们看看该方法
 public String getBase64StringWithPath(String path) throws IOException{
	   String result = "";
	   //判断是否为空
	   if(FlexTool.getInstance().StringIsNotNull(path)){
		   //可能会旋转一定的角度,需要旋转正确在上传
		   Bitmap image= getCorrectRotateBitmap(path);
		   
		   String suffix = getSuffixofImage(path);
		   //日志输出
		   LogTool.getInstance().log(suffix);
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   //判断图片格式,然后 压缩
		   int options = 100;
		   while ( baos.toByteArray().length / 1024>Config.image_up_size) {	//循环判断如果压缩后图片是否大于100kb,大于继续压缩		
			   baos.reset();//重置baos即清空baos
			   if("png".equals(suffix)){
				   image.compress(Bitmap.CompressFormat.PNG, options, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
			   }else if("jpeg".equals(suffix)){
				   image.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
			   }else{
				   image.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
			   }
			   options -= 10;//每次都减少10
		   }
		   result = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
	   }
	   return result;
   }
接下来是 取得图片格式(只读取后缀是不行的),旋转图片的方法
   private String getSuffixofImage(String path) {  
	   BitmapFactory.Options opts = new BitmapFactory.Options();
	   opts.inJustDecodeBounds = true; //确保图片不加载到内存
	   BitmapFactory.decodeFile(path, opts);
	   String result  = opts.outMimeType;
	   if(result.indexOf("/")>0){
		   return result.substring(result.indexOf("/")+1);
	   }
	   return result;
   } 


/**
	 * 读取图片属性:旋转的角度
	 * @param path 图片绝对路径
	 * @return degree旋转的角度
	 */
    private int readPictureDegree(String path) {
        int degree  = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
                e.printStackTrace();
        }
        return degree;
    }
   /*
    * 旋转图片 
    * @param angle 
    * @param bitmap 
    * @return Bitmap 
    */  
   public Bitmap getCorrectRotateBitmap(String path) {
	   
	   if(FlexTool.getInstance().StringIsNotNull(path)){
		   int angle = readPictureDegree(path);
		   Bitmap bitmap = BitmapFactory.decodeFile(path);
		   //旋转图片 动作   
		   Matrix matrix = new Matrix();;  
		   matrix.postRotate(angle);  
		   // 创建新的图片   
		   Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,  
				   bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
		   return resizedBitmap;  
	   }else{
		   return null;
	   }
   }
懒得 调格式了,很累,拍砖请留言,感觉可以优化,,

下载部分:
得到图片 地址,异步请求 该图片,并保留到本地文件夹,需要显示图片的时候 先看内存,然后本地寻找,最后去网络


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值