且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-09 13:05:52

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

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:

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