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

时间:2022-11-19 21:45:26

I am developing an application for Android, and part of the application has to takes pictures and save them to the SDcard. The onPictureTaken method returned a byte array with the data of the captured image.

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

All I need to do is save the byte array into a .jpeg image file. I have attempted to do this with the help of BitmapFactory.decodeByteArray (to get a Bitmap) and then bImage.compress (to an OutputStream), a plain OutputStream, and a BufferedOutputStream. All three of these methods seem to give me the same weird bug. My Android phone (8MP camera and a decent processor), seems to save the photo (size looks correct), but in a corrupted way (the image is sliced and each slice is shifted; or I just get almost horizontal lines of various colors); and The weird thing is, that an Android tablet with a 5MP camera and a fast processor, seems to save the image correctly.

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

So I thought maybe the processor can't keep up with saving large images, because I got OutOfMemory Exceptions after about 3 pictures (even at compression quality of 40). But then how does the built in Camera app do it, and much faster too? I'm pretty sure (from debug) that the OutputStream writes all the data (bytes) and it should be fine, but it's still corrupted.

所以我想也许处理器无法跟上保存大图像,因为我在大约3张图片后得到了OutOfMemory Exceptions(即使压缩质量为40)。但那么内置的相机应用程序是如何做到的,而且速度也快得多?我很确定(从调试)OutputStream写入所有数据(字节),它应该没问题,但它仍然被破坏。

***In short, what is the best/fastest way (that works) to save a byte array to a jpeg file?

***简而言之,将字节数组保存到jpeg文件的最佳/最快方法是什么?

Thanks in advance, Mark

马克,提前谢谢

code I've tried (and some other slight variations):

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

    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) {
    }

and

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

1 个解决方案

#1


49  

All I need to do is save the byte array into a .jpeg image file.

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

Just write it out to a file. It already is in JPEG format. Here is a sample application demonstrating this. Here is the key piece of code:

只需将其写入文件即可。它已经是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);
    }
}

#1


49  

All I need to do is save the byte array into a .jpeg image file.

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

Just write it out to a file. It already is in JPEG format. Here is a sample application demonstrating this. Here is the key piece of code:

只需将其写入文件即可。它已经是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);
    }
}