Java 如何在不丢失原始文件的情况下压缩图像?

Java 如何在不丢失原始文件的情况下压缩图像?,java,android,file,image-compression,Java,Android,File,Image Compression,在上传图像之前,我使用以下代码进行图像压缩: public File saveBitmapToFile(File file) { try { // BitmapFactory options to downsize the image BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; o.inSampleS

在上传图像之前,我使用以下代码进行图像压缩:

public File saveBitmapToFile(File file) {
    try {

        // BitmapFactory options to downsize the image
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        o.inSampleSize = 6;
        // factor of downsizing the image

        FileInputStream inputStream = new FileInputStream(file);
        //Bitmap selectedBitmap = null;
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        // The new size we want to scale to
        final int REQUIRED_SIZE = 75;

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE &&
                o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = new FileInputStream(file);

        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();


        FileOutputStream outputStream = new FileOutputStream(file);


        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);



        return file;
    } catch (Exception e) {
        return null;
    }
}
问题是原始图像将受到影响并调整大小

如何在不覆盖和丢失原始图像的情况下压缩图像

更新1:

我将代码的最后一部分更改为此,但仍然不起作用

现在,图像无法调整大小

            File new_file =new File("/storage/emulated/0/DCIM/Screenshots/tmp.png");
        try
        {
            new_file.createNewFile();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        Log.d("Create File", "File exists?"+new_file.exists());

        FileOutputStream outputStream = new FileOutputStream(new_file);

        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);


        return file;
更新2:

我把代码改成了这个,所以问题部分解决了。现在我可以在名为
tmp“+new Date()+”.png的文件中获得每个图像的原始质量,但原始文件仍将被覆盖

File new_file =new File(String.valueOf("/storage/emulated/0/DCIM/Screenshots/tmp"+new Date()+".png"));
        try
        {
            new_file.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(new_file, true);
            selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);


        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        Log.d("Create File", "File exists?"+new_file.exists());

        FileOutputStream outputStream = new FileOutputStream(file);

        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

创建一个新文件并使FileOutputStream写入其中,而不是写入原始文件。

新答案

您的案例非常独特,如果您尝试以下方法会怎么样:

与其直接使用InputStream中的位图,不如尝试使用它的
copy()

这样,压缩的将是位图的副本。您可以在新的
FileOutputStream
中压缩它,而无需修改原始文件


并移除第二次压缩。不要对原始文件做任何操作。

有什么不起作用?@MattClark我在问题中添加了更新2为什么要压缩两次?第二个仍然在使用原始文件,因此它将覆盖它。我这样做:
new_文件
将具有原始内容,原始文件将被覆盖我这样做:
new_文件
将具有原始内容,原始文件将被覆盖以查看我的新答案。希望这次能奏效。