【问题标题】:Saving bitmap to SdCard Android将位图保存到 SdCard Android
【发布时间】:2014-07-28 15:12:22
【问题描述】:

我正在编写代码将 png 文件转换回 bmp 并将其保存在 sdcard 上。这是我当前的代码。

    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 到 sdcard。但是它不会达到原始大小。关于将 PNG 转换为 BMP 的任何建议?

【问题讨论】:

  • bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out),无论何时使用此方法,保存的图像质量总是会发生变化,因此您必须通过更改方法的2参数自己尝试以获得最佳效果。
  • 我知道使用压缩方法时图像质量会发生变化。但是,我正在寻找一种将 JPEG 转换回 BMP 的方法。使用我编辑的代码,原始 BMP 大约为 80 Mb,我得到 14 KB。显然解压不起作用...可以在 Android 上转换回 BMP(从 JPEG)吗?

标签: android bitmap png compression


【解决方案1】:
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;
}

【讨论】:

  • 只是在您的回复中添加信息:Environment.getExternalStorageDirectory() 不允许您访问外部 SD 卡。
  • @greywolf82 我假设 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 将在清单中提供:)
  • @AndroidWarrior 这是另存为 .JPEG 格式。我想将 ABC.JPEG 转换为 DEF.bmp。这可能吗?使用此代码 - bitmap .compress(Bitmap.CompressFormat.JPEG, 80, out);
  • bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out) 只支持Bitmap.CompressFormat.JPEG
  • bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out) 仅支持Bitmap.CompressFormat.JPEGBitmap.CompressFormat.PNGBitmap.CompressFormat.WEBP
猜你喜欢
  • 2015-09-27
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多