将图像文件从相机压缩到一定大小

时间:2015-02-27 08:54:47

标签: android memory-management bitmap android-bitmap

我正在尝试压缩保存在文件中的图像。我正在尝试将文件压缩为1MB。我尝试了一些方法,但它通常会产生一个OutofMemoryError。 然后我尝试使用此解决方案,但它使位图空白。

  

How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

这是我的代码:

    System.gc();
    getActivity().getContentResolver().notifyChange(mImageTempUri, null);
    Bitmap bitmap;
    bitmap = BitmapFactory.decodeFile(mImageDirectory + mImageName, options);
    if(bitmap == null){
    howRequestFailedErrorMessage("Gambar gagal di-upload");
    return;

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();   


    bitmap.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
    File f = new File(mImageDirectory + mImageName);
    if(f.exists()){
        f.delete();
    }
    FileOutputStream fo;

    try {
        fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.flush();
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap.recycle();

2 个答案:

答案 0 :(得分:8)

好的,我得到了自己的答案

    File f = new File(mImageDirectory + mImageName);
    if(f.exists()){
        f.delete();
    }

    int MAX_IMAGE_SIZE = 1000 * 1024;
    int streamLength = MAX_IMAGE_SIZE;
    int compressQuality = 105;
    ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
    while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
        try {
            bmpStream.flush();//to avoid out of memory error
            bmpStream.reset();
        } catch (IOException e) {
            e.printStackTrace();
        }
        compressQuality -= 5;
        bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
        byte[] bmpPicByteArray = bmpStream.toByteArray();
        streamLength = bmpPicByteArray.length;
        if(BuildConfig.DEBUG) {
            Log.d("test upload", "Quality: " + compressQuality);
            Log.d("test upload", "Size: " + streamLength);
        }
    }

    FileOutputStream fo;

    try {
        fo = new FileOutputStream(f);
        fo.write(bmpStream.toByteArray());
        fo.flush();
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

  

科特林之路

        if (file.length() > MAX_IMAGE_SIZE) {
            var streamLength = MAX_IMAGE_SIZE
            var compressQuality = 105
            val bmpStream = ByteArrayOutputStream()
            while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
                bmpStream.use {
                    it.flush()
                    it.reset()
                }

                compressQuality -= 5
                val bitmap = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options())
                bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream)
                val bmpPicByteArray = bmpStream.toByteArray()
                streamLength = bmpPicByteArray.size
                if (BuildConfig.DEBUG) {
                    Log.d("test upload", "Quality: $compressQuality")
                    Log.d("test upload", "Size: $streamLength")
                }
            }

            FileOutputStream(file).use {
                it.write(bmpStream.toByteArray())
            }
        }
  

恒定

companion object {
    //2000 * 1024 = 2 MB
    private const val MAX_IMAGE_SIZE = 2048000
}
相关问题