Android的圖片壓縮並上傳

Android開發中上傳圖片很常見,通常爲了節省流量會進行壓縮的操做,本篇記錄一下壓縮和上傳的方法。java

圖片壓縮的方法 :android

import java.io.ByteArrayOutputStream;
import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;

public class PictureUtil {

    /**
     * 把bitmap轉換成String
     * 
     * @param filePath
     * @return
     */
    public static String bitmapToString(String filePath) {

        Bitmap bm = getSmallBitmap(filePath);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] b = baos.toByteArray();

        return Base64.encodeToString(b, Base64.DEFAULT);

    }

    /**
     * 根據路徑得到圖片並壓縮返回bitmap用於顯示
     * 
     * @param imagesrc
     * @return
     */
    public static Bitmap getSmallBitmap(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, 480, 800);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(filePath, options);
    }

    /**
     * 計算圖片的縮放值
     * 
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
    
}

 

圖片上傳的代碼:ios

/**
     * 將圖片轉成String的形式,進行上傳
     *
     * @param json
     * @return 
     * @return String  
     * @author hsx
     * @time 2014-3-21上午10:47:30
     */
    public String sendPost(String json) {
        try {
            HttpURLConnection httpcon = (HttpURLConnection) ((new URL(POST_URL)
                    .openConnection()));
            httpcon.setDoOutput(true);
            httpcon.setRequestProperty("Content-Type", "application/json");
            httpcon.setRequestProperty("Accept", "application/json");
            httpcon.setRequestMethod("POST");
            httpcon.connect();

            byte[] outputBytes = json.getBytes("UTF-8");
            OutputStream os = httpcon.getOutputStream();
            os.write(outputBytes);
            os.close();
            
            int status = httpcon.getResponseCode();
            if (status != 200) {
                throw new IOException("Post failed with error code " + status);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line+"\n");
            }
            br.close();
          
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

圖片壓縮的方式還有其餘的形式,能夠參考一下這篇文字:http://104zz.iteye.com/blog/1694762json

 

完整的demo下載地址:app

http://download.csdn.net/detail/abc13939746593/7076025
相關文章
相關標籤/搜索