Android / Java:将一个字节数组保存到一个文件(.jpeg)

我正在开发Android应用程序,部分应用程序必须拍照并将其保存到SD卡。 onPictureTaken方法返回一个包含捕获图像数据的字节数组。

我需要做的就是将字节数组保存到.jpeg图像文件中。 我试图在BitmapFactory.decodeByteArray(获取一个位图),然后bImage.compress(一个OutputStream),一个普通的OutputStream和一个BufferedOutputStream的帮助下做到这一点。 所有这三种方法似乎给了我同样的奇怪的错误。 我的Android手机(800万像素摄像头和一个不错的处理器)似乎可以保存照片(大小看起来正确),但是以损坏的方式(图像被切片并且每个切片被移位;或者我只是获得几乎水平的各种颜色的线条) ; 奇怪的是,具有500万像素摄像头和快速处理器的Android平板电脑似乎可以正确保存图像。

所以我想也许处理器不能跟上保存大图像,因为我在大约3张图片后(即使在40的压缩质量下)出现了OutOfMemory异常。 但那么内置的相机应用程序如何做到这一点,还有更快? 我很确定(从调试)OutputStream写入所有数据(字节),它应该没问题,但它仍然损坏。

***总之,什么是最好的/最快的方式(工作)将字节数组保存到JPEG文件?

在此先感谢,马克

我试过的代码(以及其他一些细微的变化):

    try {
        Bitmap image = BitmapFactory.decodeByteArray(args, 0, args.length);
        OutputStream fOut = new FileOutputStream(externalStorageFile);
        long time = System.currentTimeMillis();
        image.compress(Bitmap.CompressFormat.JPEG,
                jpegQuality, fOut);
        System.out.println(System.currentTimeMillis() - time);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
    }

    try {
        externalStorageFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(externalStorageFile);
        fos.write(args);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

我需要做的就是将字节数组保存到.jpeg图像文件中。

把它写出来写到一个文件中。 它已经是JPEG格式。 这是一个示例应用程序,演示了这一点。 这是关键的一段代码:

class SavePhotoTask extends AsyncTask<byte[], String, String> {
    @Override
    protected String doInBackground(byte[]... jpeg) {
      File photo=new File(Environment.getExternalStorageDirectory(), "photo.jpg");

      if (photo.exists()) {
            photo.delete();
      }

      try {
        FileOutputStream fos=new FileOutputStream(photo.getPath());

        fos.write(jpeg[0]);
        fos.close();
      }
      catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
      }

      return(null);
    }
}
链接地址: http://www.djcxy.com/p/46515.html

上一篇: Android/Java: Saving a byte array to a file (.jpeg)

下一篇: problem with taking pictures using the android camera