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

前端 未结 4 981
自闭症患者
自闭症患者 2020-12-14 01:11

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 wi

相关标签:
4条回答
  • 2020-12-14 01:48

    This Code is perfect for saving image in storage, from byte[]... note that "image" here is byte[]....taken as "byte[] image" as a parameter into a function.

        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);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 01:51

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 01:55

    Here's the function to convert byte[] into 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)
     {
    
     }
    }
    
    0 讨论(0)
  • 2020-12-14 02:00

    Hey this codes for 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)
                }
            }
    })
    
    0 讨论(0)
提交回复
热议问题