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 f开发者_运维百科or 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).
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
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
精彩评论