安卓:正确下载/保存电子邮件attachement [英] Android: Correctly downloading/saving an email attachement
问题描述
我有一个有趣的问题:我的应用程序被设计为发送和打开一个完整的文件压缩和压缩有一个特殊的扩展名(用户更容易)。我可以拉链拉上,我需要在一个电子邮件附加档案,我可以给他们发送。
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-邮件视图按钮,然后选择我的应用程序打开文件,它不能正常解压缩它们。不过,如果我使用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.
这是在code我用它来下载附件:
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();
}
我是不是做错了什么?提前致谢。 (另外,解压缩的过程是在两种情况下[文件浏览器和查看从电子邮件]相同,所以我pretty的确定它的东西在这里。此外,该文件不下载,并且是正确的大小。它只是不会解压缩)。
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) {
}
我只是用这个code下载附件,现在一切都完美的作品= D
I just used this code to download the attachment and now everything works perfectly =D
这篇关于安卓:正确下载/保存电子邮件attachement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!