图片和文件传输压缩总结
来源:互联网 发布:手机科研数据处理软件 编辑:程序博客网 时间:2024/05/19 13:44
转载请注明出处: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
0 0
- 图片和文件传输压缩总结
- 文件归档压缩和文件传输
- Socket编程------TCP文件传输(文档、声音、图片、视频和压缩包等)
- android图片压缩总结
- Android图片压缩总结
- android图片压缩总结
- android图片压缩总结
- android图片压缩总结
- android图片压缩总结
- android图片压缩总结
- android图片压缩总结
- Android图片 压缩总结
- Android 图片压缩总结
- android图片压缩总结
- android图片压缩总结
- 图片压缩总结
- android图片压缩总结
- Android 图片压缩总结
- java基础之多态
- GitHub的使用(下)—— 如何下载一个已存在的 Repository
- FC协议常见十问
- ASP.NET中前台调用后台、后台调用前台的变量或方法
- read it later的存在必要感
- 图片和文件传输压缩总结
- c++ protected成员及继承
- SQL语句中Where后面最多能连接多少个条件
- 算法
- Ubuntu下"由于已经达到 MaxReports 限制,没有写入 apport 报告" 错误解决办
- 2013年下半年案例挣值分析技术EVM案例试题讲解
- 漫谈varchar和Nvarchar区别
- DB2 linux 图形界面 安装
- 回调函数