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
原创粉丝点击