2011-04-26 16 views
5

जैसा कि मैंने एक प्रश्न पोस्ट कुछ दिन पहले, मुझे एहसास हुआ thet शेयर Email एप्लिकेशन अनुलग्नक में एकाधिक फ़ाइलों को नहीं भेज सकता है: https://stackoverflow.com/questions/5773006/sending-email-with-multiple-attachement-fail-with-default-email-android-app-butSDCard पर कुछ फाइलों के साथ एक ज़िप फ़ाइल बनाना

दुर्भाग्य से, मैं पर कोई जवाब नहीं मिला है यह, एक कामकाज खोजने की जरूरत है।

उपयोगकर्ता को कुछ पीडीएफ सूची में चयन करना है और उन्हें स्टॉक ऐप के साथ ईमेल द्वारा भेजना है। चूंकि एकाधिक अनुलग्नक विफल हो जाते हैं, इसलिए मैं सभी अनुरोधित फ़ाइलों के साथ एक ज़िप फ़ाइल बनाउंगा और यह अनन्य फ़ाइल भेजूंगा।

तो, क्या मैं एसडीकार्ड पर कुछ फ़ाइलों के साथ संग्रह कर सकता हूं? http://developer.android.com/reference/java/util/zip/ZipFile.html

सार्वजनिक zipfile (फ़ाइल फ़ाइल)

के बाद से:: एपीआई स्तर 1 निर्दिष्ट फ़ाइल से Constructs एक नया zipfile

यह क्या मैं वर्तमान में पाया जाता है।

लेकिन मुझे समझ में नहीं आता कि इसे एकाधिक फ़ाइलों के साथ कैसे उपयोग किया जाए।

एक बहुत धन्यवाद,

उत्तर

2

ZipFile एक फ़ाइल मामले के लिए एक शॉर्टकट है। यदि आप कई फाइलें करना चाहते हैं, तो आपको ZipOutputStream के साथ काम करने की आवश्यकता है - आपके द्वारा उद्धृत जावाडोक पेज से केवल एक क्लिक दूर।

और वह जे avadoc also has an example एकाधिक फ़ाइलों को ज़िपित करने के तरीके पर।

1

मैं एक स्थिर वर्ग में 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, 0, BUFFER)) != -1) { 
        out.write(data, 0, count); 
       } 
       origin.close(); 
      } 

      out.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
संबंधित मुद्दे