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

Android:正确下载/保存电子邮件附件,android,email,download,save,attachment,Android,Email,Download,Save,Attachment,我有一个有趣的问题:我的应用程序设计用来发送和打开一个装满文件的zip文件,而zip文件有一个特殊的扩展名(对用户来说更容易)。我可以将需要附加在电子邮件中的文件压缩,然后发送 当我使用g-mail“查看”按钮并选择我的应用程序打开文件时,它无法正确解压文件。但是,如果我使用gmail的“下载”按钮,然后通过文件浏览器打开文件,文件就会正确解压 这是我用来下载附件的代码: // get attachment try { attachment = getCo

我有一个有趣的问题:我的应用程序设计用来发送和打开一个装满文件的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();
        }
我做错什么了吗?提前谢谢。(另外,解压过程在两种情况下都是相同的[文件资源管理器和从电子邮件查看],因此我非常确定它在这里。此外,文件确实下载了,并且大小正确。它只是无法解压)。

请检查此文件:

android解压文件指南,希望它能帮助您解决问题

请查看:


android解压文件指南,希望能帮你解决问题

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

            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

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

            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

尝试使用OutputStreamWriter而不是BufferedOutputStream只需尝试将操作系统从BufferedOutputStream更改为OutputStreamWriter即可。。写操作系统(缓冲区,0,byteRead)仍然可以工作。你是对的:。。无论如何,我认为这可能是问题所在,因为我在多次试用后改用了它。好吧,无论如何谢谢你的尝试。有没有办法使用gmail应用程序的下载方法?尝试使用OutputStreamWriter而不是BufferedOutputStream试着将操作系统从BufferedOutputStream更改为OutputStreamWriter。。写操作系统(缓冲区,0,byteRead)仍然可以工作。你是对的:。。不管怎么说,我想这可能是问题所在,因为我在多次试用后改用了它。好吧,无论如何谢谢你的尝试。有没有办法使用gmail应用程序的下载方法?2。解压部分工作正常。这是因为正在保存的文件没有正确保存。解压部分工作正常。这是因为正在保存的文件没有正确保存。