将jpg保存到文件会使其混乱

时间:2012-08-13 16:06:38

标签: android bitmap save

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

enter image description here

代码:

保存:

    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());

我做错了什么?感谢。

1 个答案:

答案 0 :(得分: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;

    }
相关问题