2013-04-26 46 views
24

Tôi có một số thư mục có tệp HTML trong thư mục "nội dung" trong dự án Android của tôi. Tôi cần hiển thị các tệp HTML này từ các thư mục con của tài sản trong danh sách. Tôi đã viết một số mã về việc tạo danh sách này.Danh sách các tệp trong thư mục nội dung và thư mục con của nó

lv1 = (ListView) findViewById(R.id.listView); 
// Insert array in ListView 

// In the next row I need to insert an array of strings of file names 
// so please, tell me, how to get this array 

lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filel)); 
lv1.setTextFilterEnabled(true); 
// onclick items in ListView: 
lv1.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
     //Clicked item position 
     String itemname = new Integer(position).toString(); 
     Intent intent = new Intent(); 
     intent.setClass(DrugList.this, Web.class); 
     Bundle b = new Bundle(); 
     //I don't know what it's doing here 
     b.putString("defStrID", itemname); 
     intent.putExtras(b); 
     //start Intent 
     startActivity(intent); 
    } 
}); 

Trả lời

56
private boolean listAssetFiles(String path) { 

    String [] list; 
    try { 
     list = getAssets().list(path); 
     if (list.length > 0) { 
      // This is a folder 
      for (String file : list) { 
       if (!listAssetFiles(path + "/" + file)) 
        return false; 
       else { 
        // This is a file 
        // TODO: add file name to an array list 
       } 
      } 
     } 
    } catch (IOException e) { 
     return false; 
    } 

    return true; 
} 

Gọi listAssetFiles với tên thư mục gốc của thư mục tài sản của bạn.

listAssetFiles("root_folder_name_in_assets"); 

Nếu thư mục gốc là thư mục tài sản sau đó gọi nó với

listAssetFiles("");  
+0

mContext là gì? –

+0

nếu bạn đang làm điều này từ một hoạt động hoặc dịch vụ, sau đó mContext = getApplicationContext(); hoặc mContext = ActivityName.this; Ngoài ra bạn có thể loại bỏ nó. – Kammaar

+0

Đối tượng ngữ cảnh là – Kammaar

14

thử này nó sẽ làm việc trong trường hợp của bạn

f = getAssets().list(""); 
for(String f1 : f){ 
    Log.v("names",f1); 
} 

Đoạn trên sẽ hiển thị nội dung của thư mục gốc tài sản.

Ví dụ ... nếu bên dưới là cấu trúc tài sản ..

assets 
|__Dir1 
|__Dir2 
|__File1 

đầu ra Snippet sẽ là .... dir1 dir2 File1

Nếu bạn cần các nội dung của Thư mục Dir1

Chuyển tên thư mục vào chức năng danh sách.

f = getAssets().list("Dir1"); 
+1

Tôi đã chỉnh sửa mã này, vì Eclipse hiển thị lỗi. 'Chuỗi [] f = null; \t \t thử { \t \t \t f = getAssets(). List (""); \t \t} catch (IOException e) { \t \t \t // TODO Auto-tạo ra khối catch \t \t \t e.printStackTrace(); \t \t} \t cho (Chuỗi f1: f) { \t Log.v ("tên", f1); \t} 'này –

+1

Và kết quả là - giả lập cho tôi 4 danh sách các mục: ma túy, hình ảnh, âm thanh, webkit –

+1

Ya nó cho thấy rằng ... nhưng việc chuyển tên của thư mục youtlr bên trong tài sản có thể giúp bạn thoát khỏi nó vượt qua bên trong khung của thư mục mẹ và tất cả nội dung của nó sẽ được liệt kê ...... như getAssets(). list ("drugs"); – cafebabe1991

0

Hope Trợ giúp này:

mã sau đây sẽ sao chép tất cả các thư mục và nội dung của nó và nội dung của thư mục phụ để vị trí sdcard:

private void getAssetAppFolder(String dir) throws Exception{ 

    { 
     File f = new File(sdcardLocation + "/" + dir); 
     if (!f.exists() || !f.isDirectory()) 
      f.mkdirs(); 
    } 
    AssetManager am=getAssets(); 

    String [] aplist=am.list(dir); 

    for(String strf:aplist){ 
     try{ 
      InputStream is=am.open(dir+"/"+strf); 
      copyToDisk(dir,strf,is); 
     }catch(Exception ex){ 


      getAssetAppFolder(dir+"/"+strf); 
     } 
    } 



} 


public void copyToDisk(String dir,String name,InputStream is) throws IOException{ 
    int size; 
     byte[] buffer = new byte[2048]; 

     FileOutputStream fout = new FileOutputStream(sdcardLocation +"/"+dir+"/" +name); 
     BufferedOutputStream bufferOut = new BufferedOutputStream(fout, buffer.length); 

     while ((size = is.read(buffer, 0, buffer.length)) != -1) { 
      bufferOut.write(buffer, 0, size); 
     } 
     bufferOut.flush(); 
     bufferOut.close(); 
     is.close(); 
     fout.close(); 
} 
Các vấn đề liên quan