簡體   English   中英

Android:來自 ContentResolver 的 InputStream 已損壞

[英]Android: InputStream from ContentResolver is corrupted

在業余時間,我開發了一個 Android 應用程序,該應用程序通過 Inte.net 發送文件。 我自己開發這個應用程序,所以我不太關心關於隱私等的 Playstore Guildlines。

應用程序應該做什么:

  • 用戶單擊按鈕以從存儲中訪問 select 文件
  • Android 將該文件提供給我的應用程序 -> 這就是問題所在
  • 我的應用程序讀取該文件並發送它

問題:我的應用程序得到一個“URI”。 我嘗試使用 ContentResolver 從該 URI 讀取文件。 並將文件復制到我的應用程序的內部存儲中。 但是當我用 ImageViewer 打開這個文件時,它告訴我,這個文件已損壞。 十六進制編輯器還顯示與原始文件的差異。

權限:

<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" />

從 Android 打開文件選擇器的代碼:

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

“addAttachment”方法:

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();
    }

在十六進制編輯器中打開的原始圖像: 截圖十六進制編輯器

復制的圖像(test4.jpg): 截圖十六進制編輯器

有相似之處,但明顯不同。 我不知道為什么。

可能是因為您使用UTF8將二進制文件編碼為帶有以下行的文本

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

您的addAttachment方法看起來很奇怪

試試這個

        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) {
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM