Android:正确下载/保存电子邮件附件

发布于 2024-11-26 17:50:43 字数 1215 浏览 0 评论 0原文

我有一个有趣的问题:我的应用程序旨在发送和打开一个充满文件的 zip,并且该 zip 有一个特殊的扩展名(对用户来说更容易)。我可以将需要在电子邮件中附加的文件压缩起来,然后发送它们。

当我使用 g-mail“查看”按钮并选择我的应用程序来打开文件时,它无法正确解压缩它们。但是,如果我使用 gmail“下载”按钮,然后通过文件资源管理器打开该文件,该文件将正确解压缩。

这是我用来下载附件的代码:

// get attachment
        try {
            attachment = getContentResolver().openInputStream(
                    getIntent().getData());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // Save it
        try {
            File root = Environment.getExternalStorageDirectory();
            path = root.getPath() + "/PSattachment.psz";
            savedFile = new File(path);
            FileOutputStream fos = new FileOutputStream(savedFile, false);
            BufferedOutputStream os = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = attachment.read(buffer)) != -1) {
                os.write(buffer, 0, byteRead);
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

我做错了什么吗?提前致谢。 (此外,两种情况下的解压缩过程都是相同的[文件浏览器和从电子邮件中查看],所以我很确定这里有东西。此外,文件确实会下载,并且大小正确。它只是不会'解压)。

I have an interesting problem: My application is designed to send and open up a zip full of files, and the zip has a special extension (easier for the user). I can zip up the files I need to attach in an e-mail, and I can send them.

When I use the g-mail "view" button and select my app to open the file, it doesn't unzip them correctly. However, if I use the gmail "download" button, and then open the file through a file explorer, the file unzips correctly.

This is the code I use to download the attachment:

// get attachment
        try {
            attachment = getContentResolver().openInputStream(
                    getIntent().getData());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // Save it
        try {
            File root = Environment.getExternalStorageDirectory();
            path = root.getPath() + "/PSattachment.psz";
            savedFile = new File(path);
            FileOutputStream fos = new FileOutputStream(savedFile, false);
            BufferedOutputStream os = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = attachment.read(buffer)) != -1) {
                os.write(buffer, 0, byteRead);
            }
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

Am I doing something wrong? Thanks in advance. (Also, the process of unzipping is the same in both cases [file explorer and view from email], so I'm pretty sure it's something in here. Also, the file DOES download, and is the right size. It just won't unzip).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2024-12-03 17:50:43

我找到了答案!!!花了一段时间,但至少现在可以使用了:

            try {
            InputStream attachment = getContentResolver()
                    .openInputStream(getIntent().getData());
            savedFile = new File(Environment
                    .getExternalStorageDirectory().getAbsolutePath(),
                    "temp" + System.currentTimeMillis() + ".psz");
            FileOutputStream f = new FileOutputStream(savedFile);
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = attachment.read(buffer)) > 0) {
                f.write(buffer);
            }
            f.close();
        } catch (Exception e) {
        }

我刚刚使用此代码下载了附件,现在一切正常=D

I found the answer!!! Took a while, but at least it works now:

            try {
            InputStream attachment = getContentResolver()
                    .openInputStream(getIntent().getData());
            savedFile = new File(Environment
                    .getExternalStorageDirectory().getAbsolutePath(),
                    "temp" + System.currentTimeMillis() + ".psz");
            FileOutputStream f = new FileOutputStream(savedFile);
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = attachment.read(buffer)) > 0) {
                f.write(buffer);
            }
            f.close();
        } catch (Exception e) {
        }

I just used this code to download the attachment and now everything works perfectly =D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文