Android 图片压缩
来源:互联网 发布:香港中文大学人工智能 编辑:程序博客网 时间:2024/06/06 04:51
在网上找的效果,改动。获取本地图片的地址,然后压缩生成新的图片上传。上传完成后删除临时文件
/** * 删除temp下的压缩图片 * @author wlj * @date 2016-1-22下午2:16:10 * @param file */ private void deleteFile(String path){ File file=new File(path); if(file.isDirectory()){ File[] files = file.listFiles(); for(int i=0; i<files.length; i++){ files[i].delete(); } } } public static File scal(String path){// String path = fileUri.getPath(); File outputFile = new File(path); long fileSize = outputFile.length(); final long fileMaxSize = 200 * 1024; if (fileSize >= fileMaxSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int height = options.outHeight; int width = options.outWidth; double scale = Math.sqrt((float) fileSize / fileMaxSize); options.outHeight = (int) (height / scale); options.outWidth = (int) (width / scale); options.inSampleSize = (int) (scale + 0.5); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeFile(path, options); outputFile = new File(createImageFile().getPath()); FileOutputStream fos = null; try { fos = new FileOutputStream(outputFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!bitmap.isRecycled()) { bitmap.recycle(); }else{ File tempFile = outputFile; outputFile = new File(createImageFile().getPath()); copyFileUsingFileChannels(tempFile, outputFile); } } return outputFile; } public static Uri createImageFile(){ // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp +"_"; String temppath = Environment.getExternalStorageDirectory()+"/wloaa/Temp/"; File storageDir=new File(temppath); if (!storageDir.exists()) { storageDir.mkdir(); }// File storageDir = Environment.getExternalStoragePublicDirectory(// Environment.DIRECTORY_PICTURES); String filename=""; File image = null; try { image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); filename=temppath+"/"+imageFileName+".jpg"; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }// Save a file: path for use with ACTION_VIEW intents return Uri.fromFile(image);// return filename; } public static void copyFileUsingFileChannels(File source, File dest){ FileChannel inputChannel = null; FileChannel outputChannel = null; try { try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } finally { try { inputChannel.close(); outputChannel.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
0 0
- android图片压缩方法
- android图片压缩方法
- android图片压缩
- android图片压缩
- Android图片压缩
- android图片压缩
- Android BitmapFactory 图片压缩
- android图片压缩
- android图片压缩总结
- android图片压缩一:
- Android 图片压缩二:
- Android图片压缩总结
- Android开发压缩图片
- Android图片压缩
- android 的图片压缩
- Android关于图片压缩
- android 图片压缩
- android图片压缩上传
- mysqladmin命令修改Mysql密码
- OS及Mac开源项目和学习资料【超级全面】
- JavaScript高级程序设计学习笔记——变量、作用域和内存问题(重要)
- 使用ARToolkit时发生的DsRenderer.ax丢失错误
- 【百金轻】:雄关漫道真如铁,而今迈步从头越。
- Android 图片压缩
- iOS 开发 -- 常见坑(PCH问题)
- Linux内核孩子Input子系统
- COCOS学习笔记--即时动作ActionInstant
- Significant Community Contributions
- js怎么定义多个数组
- 闲谈前端编码解码、C#编码解码。
- Android高效加载大图、多图解决方案,有效避免程序OOM
- xp下用双网卡同时上内网和外网