'Android: InputStream from ContentResolver is corrupted

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source