将位图保存到SD卡Android

将位图保存到SD卡Android,android,bitmap,png,compression,Android,Bitmap,Png,Compression,我正在编写一个代码,将png文件转换回bmp并保存在SD卡上。这是我当前的代码 FileInputStream in; BufferedInputStream buf; try { in = new FileInputStream("File_Path_to_Read.png"); buf = new BufferedInputStream(in); byte[] bMapArray= new byte[buf.avail

我正在编写一个代码,将png文件转换回bmp并保存在SD卡上。这是我当前的代码

    FileInputStream in;
    BufferedInputStream buf;
    try {
        in = new  FileInputStream("File_Path_to_Read.png");
        buf = new BufferedInputStream(in);
        byte[] bMapArray= new byte[buf.available()];
        buf.read(bMapArray);
        Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);

        //Code segment to save on file
        int numBytesByRow = bMap.getRowBytes() * bMap.getHeight();
        ByteBuffer byteBuffer = ByteBuffer.allocate(numBytesByRow);
        bMap.copyPixelsToBuffer(byteBuffer);
        byte[] bytes = byteBuffer.array();

        FileOutputStream fileOuputStream = new FileOutputStream("File_Path_To_Save.bmp");
        fileOuputStream.write(bytes);
        fileOuputStream.close();


        if (in != null) {
        in.close();
        }
        if (buf != null) {
        buf.close();
        }


    } catch (Exception e) {

    }
我在将bMap保存到SD卡时遇到问题。我找到的所有示例都使用bMap.compress。使用此方法,我无法另存为bmp。有人能举例说明如何在SD卡上保存位图吗

编辑:
我现在可以将文件保存为.bmp到SD卡。然而,它不会达到原来的大小。关于将PNG转换为BMP的任何建议?

只是为了向回复中添加信息:Environment.getExternalStorageDirectory不允许您访问外部SD卡。@greywolf82我假设清单中将提供该建议:@AndroidWarrior这是以.JPEG格式保存的。我想将ABC.JPEG转换为DEF.bmp。这可能吗?使用此代码-bitmap.compressBitmap.CompressFormat.JPEG,80,out;bitmap.compressBitmap.CompressFormat.JPEG,80,out-It ony支持bitmap.CompressFormat.JPEG.CompressFormat.JPEG,80,out-It ony支持bitmap.CompressFormat.JPEG,bitmap.CompressFormat.PNG,bitmap.CompressFormat.WEBPbitmap.compressBitmap.CompressFormat.JPEG,80,out,无论何时使用此方法,保存的图像质量总是会发生变化,因此您必须通过更改方法的2参数来尝试最佳匹配。我知道使用压缩方法时图像质量会发生变化。然而,我正在寻找一种将JPEG格式转换回BMP格式的方法。原始的BMP是80 Mb,使用我编辑的代码,我得到14 KB。很明显,解压不起作用…可以在Android上将JPEG格式转换回BMP格式吗?
File root = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "My_Folder" + File.separator);
    root.mkdirs();
    File myFile = new File(root, "ABC.jpg");
    Bitmap bitmap = decodeFile(myFile, 800, 600);
    OutputStream out = null;
    File file = new File(mediaStorageDir.getAbsoluteFile() + "/DEF.jpg");
    try {
        out = new FileOutputStream(file);
        bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out);
        out.flush();
        out.close();
        bitmap.recycle();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

public static Bitmap decodeFile(File f, int WIDTH, int HIGHT) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // The new size we want to scale to
        final int REQUIRED_WIDTH = WIDTH;
        final int REQUIRED_HIGHT = HIGHT;
        // Find the correct scale value. It should be the power of 2.
        int scale = 2;
        while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
return null;
}