Android:压缩一个位图,几乎没有ram滥用

时间:2014-12-09 23:03:52

标签: android bitmap compression jpeg

当我打电话

BitmapFactory.decodeFile(filepath);

我的内存不足是因为文件太大了。 如何在不滥用ram且不缩放ram的情况下压缩此文件? 请帮帮我=(

这就是我现在可以做的事情(我在堆栈中找到了这段代码)

bm = BitmapFactory.decodeFile(filepath);
    File compressedFilePicture;
    try {
        compressedFilePicture = createImageFile();
        FileOutputStream fOut = new FileOutputStream(compressedFilePicture);
        bm.compress(CompressFormat.JPEG, COMPRESS_VALUE, fOut);
        new File(filepath).delete();
        filepath = compressedFilePicture.getAbsolutePath();
        bm = BitmapFactory.decodeFile(filepath);
        imageview.setImageBitmap(bm);
        sc.string = filepath;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我需要在ram上加载位图一次...任何想法??

1 个答案:

答案 0 :(得分:0)

    bm = BitmapFactory.decodeFile(filepath);
    File compressedFilePicture;
    try {
        compressedFilePicture = createImageFile();
        FileOutputStream fOut = new FileOutputStream(compressedFilePicture);
        bm.compress(CompressFormat.JPEG, COMPRESS_VALUE, fOut);
        new File(filepath).delete();
        filepath = compressedFilePicture.getAbsolutePath();
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 4;  //u can use 2/4/8/16....ect
Uri uri = getImageUri(filepath);     
InputStream in = in = contentResolver.openInputStream(uri);

bm = BitmapFactory.decodeStream(in, null, options);
        imageview.setImageBitmap(bm);
        sc.string = filepath;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }