正如我几天前发布的一个问题,我意识到股票电子邮件应用程序无法发送附件中的多个文件:
https://stackoverflow.com/questions/5773006/sending-email-with-multiple-attachement-fail-with-default-email-android-app-but
不幸的是,我没有得到答案,所以需要找到一个解决方法.
用户必须在列表中选择一些pdf并通过电子邮件发送它们与股票应用程序.由于多个附件失败,我将创建一个包含所有请求文件的zip文件,并发送此唯一文件.
那么,我可以在SDCard上使用一些文件进行存档吗?
我目前发现的是:http://developer.android.com/reference/java/util/zip/ZipFile.html
public ZipFile (File file)
Since: API Level 1 Constructs a new
ZipFile with the specified file.
但我不明白如何使用多个文件.
非常感谢,
解决方法
我将
the code from the static link answer修改为静态类,但这对我很有用:
import android.util.Log; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Zipper { private static final int BUFFER = 2048; public static void zip(String[] files,String zipFile) { try { BufferedInputStream origin = null; FileOutputStream dest = new FileOutputStream(zipFile); ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); byte data[] = new byte[BUFFER]; for (int i = 0; i < files.length; i++) { Log.v("Compress","Adding: " + files[i]); FileInputStream fi = new FileInputStream(files[i]); origin = new BufferedInputStream(fi,BUFFER); ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); out.putNextEntry(entry); int count; while ((count = origin.read(data,BUFFER)) != -1) { out.write(data,count); } origin.close(); } out.finish(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }