Home > Software design >  Android: InputStream from ContentResolver is corrupted
Android: InputStream from ContentResolver is corrupted

Time:02-17

In my Sparetime I work on an Android App that send's Files over the Internet. I develop this App on my own, so I'm not too concerned with Playstore Guildlines regarding Privacy and so on.

What the App should do:

  • user klicks Button to select File from Storage
  • Android gives that File to my App -> Here is the Problem
  • My App reads that File and send's it

The Problem: My App get's an "URI". I try to read the File from that URI using the ContentResolver. And Copy the File in the internal Storage of my App. But when I open this file with an ImageViewer it tells me, that this file is corrupt. A Hex-Editor also shows differences from the original File.

Permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

Code to open the File-Chooser from Android:

ActivityResultLauncher<String> launcherGetContent = registerForActivityResult(
            new ActivityResultContracts.GetContent(),
            result ->
            {
                if (result != null)
                {
                    addAttachment(result);
                }
            }
    );
    
    launcherGetContent.launch("*/*");

The "addAttachment" Methode:

try
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        InputStream inputStream = context.getContentResolver().openInputStream(uri);

        int read = -1;
        byte[] data = new byte[1024];


        while ((read = inputStream.read(data, 0, data.length)) != -1)
        {
            byteArrayOutputStream.write(data, 0, read);
        }

        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();

        BufferedWriter buf = new BufferedWriter(new FileWriter(context.getApplicationInfo().dataDir   "/cache/test4.jpg"));
        buf.write(byteArrayOutputStream.toString("UTF-8"));
        buf.flush();
        buf.close();

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

The Original Image opened in a Hex-Editor: Screenshot Hex-Editor

The Copied Image (test4.jpg): Screenshot Hex-Editor

There are Similarities, but clearly differences. And I don't know why.

CodePudding user response:

May be because you have UTF8 encoded a binary file as text with the line

buf.write(byteArrayOutputStream.toString("UTF-8"));

Your addAttachment method looks weird

try this instead

        File cacheFile = new File(getCacheDir(), "test4.jpg");

        // Now read the file
        try{
            InputStream input = context.getContentResolver().openInputStream(uri);
            int originalSize = input.available();

            bis = new BufferedInputStream(input);
            bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
            byte[] buf = new byte[originalSize];
            //noinspection ResultOfMethodCallIgnored
            bis.read(buf);
            do {
                bos.write(buf);
            } while (bis.read(buf) != -1);

        } catch (Exception e) {
            // Notify User of fail
            Log.e(Constants.TAG, "readFile:"   e); 
        } finally {
            try {
                if (bos != null) {
                    bos.flush();
                    bos.close();
                }
            } catch (Exception ignored) {
            }
        }

  • Related