Android-画像アップロード圧縮ツール類

4337 ワード

package cn.guyu.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

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 {

	
	
	/**
	 * 1.    
	 * @param image
	 * @return
	 */
	public static Bitmap compressImage(Bitmap image) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//      ,  100     ,          baos 
		int options = 100;
		while ( baos.toByteArray().length / 1024>100) {	//               100kb,      		
			baos.reset();//  baos   baos
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);//    options%,          baos 
			options -= 10;//     10
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//       baos   ByteArrayInputStream 
		
		
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// ByteArrayInputStream      
		return bitmap;
	}
	
	
	/**
	 * 2           
	 * @param srcPath      
	 * @return
	 */
	public static Bitmap getimage(String srcPath) {
		BitmapFactory.Options newOpts = new BitmapFactory.Options();
		//      ,   options.inJustDecodeBounds   true 
		newOpts.inJustDecodeBounds = true;
	/*	        true,  BitmapFactory.decodeFile(String path, Options opt)         Bitmap  ,
		        ,      ,            ,           OOM 。*/
		Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//    bm  
		
		newOpts.inJustDecodeBounds = false;
		int w = newOpts.outWidth;
		int h = newOpts.outHeight;
		//        800*480   
		float hh = 800f;//       800f
		float ww = 480f;//       480f
		//   。         ,                  
		int be = 1;//be=1     
		if (w > h && w > ww) {//                 
			be = (int) (newOpts.outWidth / ww);
		} else if (w < h && h > hh) {//                 
			be = (int) (newOpts.outHeight / hh);
		}
		if (be <= 0)
			be = 1;
		newOpts.inSampleSize = be;//      
		//      ,       options.inJustDecodeBounds   false 
		
		bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
		
		//       
		return compressImage(bitmap);//               
	}
	
	/**
	 * 3        
	 * @param filePath
	 * @return 
	 * @return
	 * @throws IOException 
	 */
	public static String bitmapToPath(String filePath) throws IOException {

		Bitmap bm = getimage(filePath);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);
		
		//     
		String imgName=getfilepath(filePath);
		//      
		String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/ImgTmp";
		//   sdcard    
		
	    File parent =new File(sdPath);
	    if(!parent.exists()){
	    	//    
	    	parent.mkdirs();
	    }
		//       
        File file =new File(parent,imgName);
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baos.toByteArray());
        fos.flush();
        fos.close();
        baos.close();
        //      
		return sdPath+"/"+imgName;
		
	}

	
	/**
	 * 
	 * @param path
	 * @return
	 */
	private static String getfilepath(String path){
		return System.currentTimeMillis()+getExtensionName(path);
	}
	
	
	/*
	 * 4       
	 */
	private static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot, filename.length());
			}
		}
		return filename;
	}

	/**
	 * 5      
	 * 
	 * @param imgs
	 */
	public static void deleteImgTmp(List<String> imgs) {

		for (String string : imgs) {
			File file = new File(string);
			if (file.exists()) {
				file.delete();
			}
		}

   }
   
	
}

//ピクチャは縮小してから質的な圧縮を経て、(呼び出し方法2)圧縮後にアップロードし、圧縮後の一時パスを取得し、アップロード後に呼び出して一時パス下のピクチャを削除することもできる.
(私の前のようなモーメンツに写真を追加して、出会った問題は今日も解決しました.gridViewをクリックすると、黄色い枠が出てきます.Selectorが設定した属性のせいです)GridViewの中で1つの属性android:listSelector="@color/baise"だけでこの問題を解決できます.