0

I'm trying to create an app that will save image from a Byte[] Array as a .png file into my smb2 server, I was able to save a file but it only contains the Array as file name with 0kb size.

Getting image from camera

@SuppressLint("MissingSuperCall")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == REQUEST_CODE) {
            if (resultCode != RESULT_CANCELED) {
                image = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                bytesCapturedLogbook = byteArrayOutputStream.toByteArray();

                MyCopy my = new MyCopy();
                my.execute(bytesCapturedLogbook);
            }
        }
    }

Class for inserting file into my server

private class MyCopy extends AsyncTask<byte[], String, String> {

        @Override
        protected String doInBackground(byte[]... bytes) {
            String z = "";
            try {

                String url = "smb://000.000.0.000/spm/Image/" + bytes + ".png";

                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                        null, "********", "**********");
                SmbFile sfile = new SmbFile(url, auth);

                if (!sfile.exists()) {
                    sfile.createNewFile();
                    z = "Created the file for you!!!!";
                } else
                    z = "Already exists at the specified location!!!!";

            } catch (Exception ex) {
                // TODO: handle exception
                z = ex.getMessage().toString();
            }
            return z;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub


        }

        @Override
        protected void onPostExecute(String r) {
        }


    }

Result inside file explorer

enter image description here

Ryan Baltazar
  • 15
  • 1
  • 5
  • 1
    I don't see anywhere that you actually attempt to write any bytes to file. See here for how this could be done: [byte\[\] to file in Java](https://stackoverflow.com/questions/4350084/byte-to-file-in-java) or see [this answer using newer NIO features](https://stackoverflow.com/a/29623982/1270000) – sorifiend Aug 16 '22 at 06:05
  • There's no file getting saved, when I tried all the answers from the link you provided. – Ryan Baltazar Aug 16 '22 at 06:57
  • doInBackground: java.io.FileNotFoundException: 004920224616080824: open failed: EROFS (Read-only file system) 004920224616080824 is the file name – Ryan Baltazar Aug 16 '22 at 07:06
  • It would help if you updated the question with the actions that save the `bytes` parameter of `doBackground`. At present this creates empty file. By the way the parameter is defined as `byte[][]` due to the `...`. – DuncG Aug 16 '22 at 07:20
  • https://stackoverflow.com/questions/71394187/take-and-save-photo-to-network-folder-android, I updated my code like this, but I get 'java.io.FileNotFoundException' – Ryan Baltazar Aug 17 '22 at 06:41
  • Unfortunately we can't guess which bits of the other question you've copied. I've voted to close (needs debugging details) because your program seems to work exactly as you've described: it intentionally creates an empty file. – DuncG Aug 17 '22 at 08:01
  • I'm only a beginner, I really don't know what I supposed to do, hoping someone can help me. – Ryan Baltazar Aug 18 '22 at 08:30
  • @Ryan Baltazar I've added suggestions in an answer below which may help, though this question may get closed soon. BTW You've used `bytes + ".png"` as the target file which means you have a weird filename `[[B...`. – DuncG Aug 18 '22 at 09:09

1 Answers1

0

Your question may be partly answered in the question you found. It shows file to smbfile copy via byte[]. As you have byte[] already you should adjust the doInBackground(byte[]... bytes) to pass your single byte[] to smbfile something like this:

SmbFileOutputStream outNew = new SmbFileOutputStream(smbFile);
outNew.write(bytes[0]);
outNew.close();

or in later JVM its better with with try-with-resources to clean up for you:

try (SmbFileOutputStream outNew = new SmbFileOutputStream(smbFile)) {
   outNew.write(bytes[0]);
}

Note that I'm not able to test any of the above, and as you've also shown the problem java.io.FileNotFoundException: 004920224616080824: open failed: EROFS then you may be simply trying to write to an invalid location (a path issue?) as it looks like you put the wrong file names into new SmbFile(...).

DuncG
  • 12,137
  • 2
  • 21
  • 33