Saving Byte Array as a PNG file into smb2 server

213 Views Asked by At

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

0

There are 0 best solutions below