2012-06-18 13 views
6

मेरे पास एक फ़ोल्डर है जिसमें कुछ फ़ाइलें और कुछ निर्देशिकाएं हैं जिन्हें मुझे लॉन्च करते समय अपने एसडी कार्ड/एमएनटी/एसडीकार्ड/एंड्रॉइड/डेटा/पथ पर कॉपी करने की आवश्यकता है पहली बार आवेदन, और निश्चित रूप से, यदि पहले से आवश्यक फ़ोल्डर उस पथ में मौजूद नहीं है।रेस/कच्चे फ़ोल्डर से एसडी कार्ड में निर्देशिकाओं और फ़ाइलों की प्रतिलिपि बनाना - एंड्रॉइड

मेरे पास यह एप्लिकेशन मेरे एप्लिकेशन के res/raw फ़ोल्डर के अंदर होगा।

चरण-दर-चरण प्रक्रियाओं के लिए मुझे क्या करने की ज़रूरत है, ताकि मैं फ़ोल्डर और उसकी सभी सामग्री को एसडी कार्ड में निर्दिष्ट पथ में res/raw से कॉपी कर सकूं।

किसी भी मदद की बहुत सराहना की जाती है।

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    copyFileOrDir("edu1");//directory name in assets 
} 
File sdCard = Environment.getExternalStorageDirectory(); 
private void copyFileOrDir(String path) { 
    AssetManager assetManager = this.getAssets(); 
    String assets[] = null; 
    try { 
     assets = assetManager.list(path); 
     if (assets.length == 0) { 
      copyFile(path); 
     } else { 
      File dir = new File (sdCard.getAbsolutePath() + "/" + "Android/data"); 
      //String fullPath = "/data/data/" + this.getPackageName() + "/" + path;//path for storing internally to data/data 
      //File dir = new File(fullPath); 
      if (!dir.exists()){ 
       System.out.println("Created directory"+sdCard.getAbsolutePath() + "/Android/data"); 
       boolean result = dir.mkdir(); 
       System.out.println("Result of directory creation"+result); 
      } 

      for (int i = 0; i < assets.length; ++i) { 
       copyFileOrDir(path + "/" + assets[i]); 
      } 
     } 
    } catch (IOException ex) { 
     System.out.println("Exception in copyFileOrDir"+ex); 
    } 
} 

private void copyFile(String filename) { 
    AssetManager assetManager = this.getAssets(); 

    InputStream in = null; 
    OutputStream out = null; 
    try { 
     in = assetManager.open(filename); 
     //String newFileName = "/data/data/" + this.getPackageName() + "/" + filename;//path for storing internally to data/data 
     String newFileName = sdCard.getAbsolutePath() + "/Android/data/" + filename; 
     out = new FileOutputStream(newFileName); 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) { 
      out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     System.out.println("Exception in copyFile"+e); 
    } 

} 
} 
+0

क्या आप अब तक जो कुछ भी कर चुके हैं उस पर कुछ कोड पोस्ट करना चाहते हैं? ऐसा लगता है कि आप बस इतना कह रहे हैं "मैं यही करना चाहता हूं, अब यह मेरे लिए कोड करें।" –

+0

वास्तव में नहीं, मैं अपना प्रश्न संपादित कर रहा हूं। – user1400538

उत्तर

2

मैं सुझाव है कि आप संपत्ति में अपनी फ़ाइलें रखें:

संपादित

निम्नलिखित समाधान अगर यह किसी और में मदद करता है। निम्न कोड आपको संपत्ति निर्देशिका से एसडी कार्ड में सामग्री कॉपी करने में मदद कर सकता है।

public static void copyFile(Activity c, String filename) 
{ 
    AssetManager assetManager = c.getAssets(); 

    InputStream in = null; 
    OutputStream out = null; 
    try 
    { 
     in = assetManager.open(filename); 
     String newFileName = sdcardpath/filename; 
     out = new FileOutputStream(newFileName); 

     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) 
     { 
      out.write(buffer, 0, read); 
     } 
     in.close(); 
     in = null; 
     out.flush(); 
     out.close(); 
     out = null; 
    } catch (Exception e) { 
     Utility.printLog("tag", e.getMessage()); 
    }finally{ 
     if(in!=null){ 
      try { 
       in.close(); 
      } catch (IOException e) { 
       printLog(TAG, "Exception while closing input stream",e); 
      } 
     } 
     if(out!=null){ 
      try { 
       out.close(); 
      } catch (IOException e) { 
       printLog(TAG, "Exception while closing output stream",e); 
      } 
     } 
    } 
} 
+0

जैसा कि मैंने अपने कस्टियन में स्पष्ट रूप से उल्लेख किया है, यह अकेले फाइल नहीं है जिसे मुझे कॉपी करने की आवश्यकता है, लेकिन फ़ोल्डर्स और उप फ़ोल्डर्स भी – user1400538

+0

यह लिंक आपको कच्चे संसाधन http://stackoverflow.com/questions/939170/ से संसाधन कॉपी करने में मदद करेगा। संसाधन-openrawresource-issue-android – vineet

+0

यदि आपको उप-फ़ोल्डर को संपत्तियों से भी कॉपी करने की आवश्यकता है, तो आपको कॉपी को थोड़ा सा संशोधित करने की आवश्यकता है। – vineet

संबंधित मुद्दे