您的位置:首页 > 移动开发 > Android开发

Android 图片压缩

2016-01-22 15:47 507 查看
在网上找的效果,改动。获取本地图片的地址,然后压缩生成新的图片上传。上传完成后删除临时文件

/**
* 删除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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android