图片和文件传输压缩总结
转载请注明出处:http://blog.csdn.net/krislight/article/
1.壓縮圖片工具类
public class CompressImage { private Bitmap bm; private String filePath; public CompressImage(String filePath) { this.filePath = filePath; } public Bitmap getBitmap() throws Exception{ BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; bm = BitmapFactory.decodeFile(filePath, opt); int picWidth = opt.outWidth; int picHeight = opt.outHeight; WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); int screenWidth = display.getWidth(); int screenHeight = display.getHeight(); opt.inSampleSize = 1; if (picWidth > picHeight) { if (picWidth > screenWidth) opt.inSampleSize = picWidth / screenWidth; } else { if (picHeight > screenHeight) opt.inSampleSize = picHeight / screenHeight; } opt.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(filePath, opt); return compressImage(bm,PIC_TRANSFORM_SIZE); } private Bitmap compressImage(Bitmap image,int size) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; while (baos.toByteArray().length/1024 > size) { baos.reset(); image.compress(Bitmap.CompressFormat.JPEG, options, baos); options -= 10; } if(image!=null){ if(!image.isRecycled()){ image.recycle(); } image = null; } ByteArrayInputStream isBm = new ByteArrayInputStream( baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } } }
使用步骤:
CompressImage compress=new CompressImage(m_strFileFullPath); bitmap = compress.getBitmap(); if(bitmap !=null){ String tSDPath = getExternalFilesDir(null).getAbsolutePath(); File fileDir = new File(tSDPath,"/SendFileTemp/"); if (!fileDir.exists()) { fileDir.mkdirs(); } String path = tSDPath + "/SendFileTemp/"+System.currentTimeMillis()+".jpg"; FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); file = new File(path); }
2.壓縮文件工具類
public static void zipFile(String oriFilePath, String zipFilePath) throws IOException { BufferedInputStream origin = null; ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(zipFilePath))); try { byte data[] = new byte[1000]; FileInputStream fi = new FileInputStream(oriFilePath); origin = new BufferedInputStream(fi, 1000); try { ZipEntry entry = new ZipEntry(oriFilePath.substring(oriFilePath .lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, 1000)) != -1) { out.write(data, 0, count); } } finally { origin.close(); } } finally { out.close(); } }
使用时:
String tSDPath = getExternalFilesDir(null).getAbsolutePath(); File fileDir = new File(tSDPath,"/SendFileTemp/"); if (!fileDir.exists()) { fileDir.mkdirs(); } String path = tSDPath + "/SendFileTemp/"+System.currentTimeMillis()+".zip"; Utils.zipFile(m_strFileFullPath, path); file = new File(path);
更多压缩技术参考
http://snowolf.iteye.com/blog/465433