Android:正确下载/保存电子邮件附件
我有一个有趣的问题:我的应用程序旨在发送和打开一个充满文件的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了答案!!!花了一段时间,但至少现在可以使用了:
我刚刚使用此代码下载了附件,现在一切正常=D
I found the answer!!! Took a while, but at least it works now:
I just used this code to download the attachment and now everything works perfectly =D
请检查一下:
)
http://www.jondev.net/articles/Unzipping_Files_with_Android_(以编程方式 android 中解压文件的指南,希望它能帮助解决您的问题
Check this out please:
http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
A guide to unzip files in android, hope it helps solve your problem