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

时间:2011-11-02 10:33:17

标签: android bytearray jpeg android-camera

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

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

所以我想也许处理器无法跟上保存大图像,因为我在大约3张图片后得到了OutOfMemory Exceptions(即使压缩质量为40)。但那么内置的相机应用程序是如何做到的,而且速度也快得多?我很确定(从调试中)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();
    }

4 个答案:

答案 0 :(得分:54)

  

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

只需将其写入文件即可。它已经是JPEG格式。 Here is a sample application demonstrating this。以下是关键代码:

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 :(得分:0)

此代码非常适合从字节[]中将图像保存到存储中... 请注意,这里的“图像”是byte []....。作为“ byte [] image”作为函数的参数。

    File photo=new File(Environment.getExternalStorageDirectory(), "photo.jpg");

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

    try {
        FileOutputStream fos=new FileOutputStream(photo.getPath());
        Toast.makeText(this, photo.getPath(), Toast.LENGTH_SHORT).show();
        fos.write(image);
        fos.close();
    }
    catch (java.io.IOException e) {
        Log.e("PictureDemo", "Exception in photoCallback", e);
    }
}

答案 2 :(得分:0)

嘿,这是Kotlin的代码

camera.addCameraListener(object : CameraListener(){

        override fun onPictureTaken(result: PictureResult) {
            val jpeg = result.data //result.data is a ByteArray!
            val photo = File(Environment.getExternalStorageDirectory(), "/DCIM/androidify.jpg");
            if (photo.exists()) {
                photo.delete();
            }
            try {
                val fos = FileOutputStream(photo.getPath() );
                fos.write(jpeg);
                fos.close();
            }
            catch (e: IOException) {
                Log.e("PictureDemo", "Exception in photoCallback", e)
            }
        }
})

答案 3 :(得分:0)

这是将byte []转换为image.jpg的功能

public void SavePhotoTask(byte [] jpeg){
 File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Life Lapse");
 imagesFolder.mkdirs();

 final File photo= new File(imagesFolder, "name.jpg");
 try
 {
    FileOutputStream fos=new FileOutputStream(photo.getPath());
    fos.write(jpeg);
    fos.close();
 }
 catch(Exception e)
 {

 }
}
相关问题