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

Android: Correctly downloading/saving an email attachement(Android:正确下载/保存电子邮件附件)
本文介绍了Android:正确下载/保存电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

我做错了吗?提前致谢.(此外,在这两种情况下 [文件浏览器和从电子邮件查看] 的解压缩过程是相同的,所以我很确定它在这里.此外,文件确实下载,并且大小合适.它只是赢了t 解压).

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).

推荐答案

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

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

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

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

这篇关于Android:正确下载/保存电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)