0

当我将 jpg 图像(具有白色背景)保存并加载到外部存储时,它看起来像这样:

在此处输入图像描述

编码:

节省:

    try {
        outStream = new FileOutputStream(fileUri);
        image.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();

    } catch (Exception e) {
        Log.e("cache", e.getMessage());
        e.printStackTrace();
    }

加载:

Bitmap bm = BitmapFactory.decodeFile(fileUri.toString());

我究竟做错了什么?谢谢。

4

1 回答 1

0

这是我使用的代码(应该适合你):

保存位图:

public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;
        // Use the compress method on the Bitmap object to write image to the OutputStream
        try {
            fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
            // Writing the bitmap to the output stream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();


        } 
        catch (IOException e) {
            e.printStackTrace();


        }

    }

并阅读:

public Bitmap readBitmapFromMemory(String filename) {
        Bitmap defautBitmap = null;
        File filePath = game.getFileStreamPath(filename);
        FileInputStream fi;
        try {
            fi = new FileInputStream(filePath);
            defautBitmap = BitmapFactory.decodeStream(fi);

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();

        }

        return defautBitmap;

    }
于 2012-08-13T16:48:25.077 回答