2009-09-30 14 views
5

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

और मैं संपत्ति निर्देशिका पेड़ को मुद्रित करने के लिए निम्न विधि का उपयोग कर रहा हूं।

void displayFiles (AssetManager mgr, String path) { 
    try { 
     String list[] = mgr.list(path); 
     if (list != null) 
      for (int i=0; i<list.length; ++i) 
       { 
        Log.v("Assets:", path +"/"+ list[i]); 
        displayFiles(mgr, path + list[i]); 
       } 
    } catch (IOException e) { 
     Log.v("List error:", "can't list" + path); 
    } 

} 

मेरे गतिविधि के onCreate विधि से मैं निम्नलिखित है:

final AssetManager mgr = getAssets();  
displayFiles(mgr, "/assets"); 
displayFiles(mgr, "./assets"); 
displayFiles(mgr, "/"); 
displayFiles(mgr, "./"); 

कौन मेरा पीछा उत्पादन

 
09-29 20:08:27.843: DEBUG/GFlash(6543): //AndroidManifest.xml 
09-29 20:08:27.954: DEBUG/GFlash(6543): //META-INF 
09-29 20:08:28.063: DEBUG/GFlash(6543): //assets 
09-29 20:08:28.233: DEBUG/GFlash(6543): //classes.dex 
09-29 20:08:28.383: DEBUG/GFlash(6543): //com 
09-29 20:08:28.533: DEBUG/GFlash(6543): //res 
09-29 20:08:28.683: DEBUG/GFlash(6543): //resources.arsc 

धन्यवाद देता अग्रिम में!

जॉन

उत्तर

13

उह। समस्या डिस्प्लेफाइल विधि में थी, यह निर्देशिका और फ़ाइल नाम के बीच विभाजक, "/" गायब था। क्षमा करें अगर मैंने किसी के समय बर्बाद कर दिया। DisplayFiles का एक सही संस्करण नीचे है।

void displayFiles (AssetManager mgr, String path) { 
    try { 
     String list[] = mgr.list(path); 
     if (list != null) 
      for (int i=0; i<list.length; ++i) 
       { 
        Log.v("Assets:", path +"/"+ list[i]); 
        displayFiles(mgr, path + "/" + list[i]); 
       } 
    } catch (IOException e) { 
     Log.v("List error:", "can't list" + path); 
    } 

} 

जॉन

+1

अपने प्रश्न में चिह्नित करें। – Matthias

+1

मैंने कोशिश की। यह मुझे बताता है कि मैं कल तक अपना जवाब स्वीकार नहीं कर सकता। –

+2

यह रूट फ़ोल्डर में सभी चीजें दिखा रहा है लेकिन मैं वास्तव में अपने संपत्ति फ़ोल्डर में से किसी भी फाइल को नहीं देख सकता, कभी भी इसे काम करने के लिए मिलता है? – schwiz

10

पूरी तरह से पुनरावर्ती आप निम्नलिखित के रूप में विधि को अपडेट कर सकते हैं होने के लिए:

void displayFiles (AssetManager mgr, String path, int level) { 

    Log.v(TAG,"enter displayFiles("+path+")"); 
    try { 
     String list[] = mgr.list(path); 
     Log.v(TAG,"L"+level+": list:"+ Arrays.asList(list)); 

     if (list != null) 
      for (int i=0; i<list.length; ++i) 
       { 
        if(level>=1){ 
         displayFiles(mgr, path + "/" + list[i], level+1); 
        }else{ 
         displayFiles(mgr, list[i], level+1); 
        } 
       } 
    } catch (IOException e) { 
     Log.v(TAG,"List error: can't list" + path); 
    } 

} 

फिर फोन के साथ: जवाब के रूप में

final AssetManager mgr = applicationContext.getAssets(); 
displayFiles(mgr, "",0);  
संबंधित मुद्दे

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