安卓/ Java的:保存一个字节数组到一个文件(JPEG格式)数组、字节、格式、文件

2023-09-05 07:55:58 作者:不可 替代的爱 〓

我为Android开发应用程序,而应用程序的一部分,具有拍摄照片,并将它们保存到SD卡。所述onPictureTaken方法返回一个字节数组,所捕获的图像的数据。

所有我需要做的是字节数组保存到一个.JPEG图像文件。我试图用BitmapFactory.de codeByteArray的帮助下做到这一点(得到一个位图),然后bImage.com preSS(到OutputStream),一个普通的OutputStream和的BufferedOutputStream。这三种方法似乎给我奇怪的错误是一样的。我的Andr​​oid手机(800万像素的摄像头和一个体面的处理器),似乎保存照片(尺寸看起来是正确的),但在一个损坏的方式(图像切片,每片移位,或者我只是得到各种颜色的几乎水平线) ;而奇怪的是,这是Android平板电脑与一个500万像素的摄像头和一个快速的处理器,似乎正确保存图像。

所以,我想,也许处理器无法与节约大量图片跟上,因为我得到了OutOfMemory异常后约3张照片(甚至在40 COM pression质量)。但随后如何在内置摄像头的应用程序做,更快呢?我是pretty的肯定(从调试)的OutputStream中写入的所有数据(字节),它应该是罚款,但它仍然损坏。

***总之,什么是最好的/最快的方式(即作品)保存一个字节数组为JPEG文件?

在此先感谢, 马克

code我试过(和其他一些轻微的变化):

 尝试{
        位图图像= BitmapFactory.de codeByteArray(参数,0,args.length);
        的OutputStream FOUT =新的FileOutputStream(externalStorageFile);
        很长一段时间= System.currentTimeMillis的();
        image.com preSS(Bitmap.Com pressFormat.JPEG,
                jpegQuality,FOUT);
        的System.out.println(System.currentTimeMillis的() - 时间);
        fOut.flush();
        fOut.close();
    }赶上(例外五){
    }
 

 尝试{
        externalStorageFile.createNewFile();
        FileOutputStream中FOS =新的FileOutputStream(externalStorageFile);
        fos.write(参数);
        fos.flush();
        fos.close();
    }赶上(例外五){
        e.printStackTrace();
    }
 

解决方案   

所有我需要做的是字节数组保存到一个.JPEG图像文件。

只是把它写出来的文件。这已经是JPEG格式。 下面是一个示例应用程序展示这种。这里是code中的关键部分:

 类SavePhotoTask扩展的AsyncTask<字节[],字符串,字符串> {
    @覆盖
    保护字符串doInBackground(字节[] ... JPEG){
      档案照片=新的文件(Environment.getExternalStorageDirectory(),photo.jpg);

      如果(photo.exists()){
            photo.delete();
      }

      尝试 {
        FileOutputStream中FOS =新的FileOutputStream(photo.getPath());

        fos.write(JPEG [0]);
        fos.close();
      }
      赶上(java.io.IOException异常E){
        Log.e(PictureDemo,异常的photoCallback,E);
      }

      返程(空);
    }
}
 
Java开发者常用工具汇总

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.

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.

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.

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

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

解决方案

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

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:

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