2013-04-26 3 views
24

Android 프로젝트의 "assets"폴더에 HTML 파일이있는 폴더가 있습니다. 애셋의 하위 폴더에있는 HTML 파일을 목록에 표시해야합니다. 이미이 목록을 작성하는 것에 대한 몇 가지 코드를 작성했습니다.assets 폴더 및 하위 폴더에있는 파일 목록

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); 
    } 
}); 

답변

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; 
} 

통화 자산 폴더의 루트 폴더 이름으로 listAssetFiles.

listAssetFiles("root_folder_name_in_assets"); 

루트 폴더는 자산 폴더는 다음

listAssetFiles("");  
+0

mContext는 무엇입니까? –

+0

활동 또는 서비스에서이 작업을 수행하는 경우 mContext = getApplicationContext(); 또는 mContext = ActivityName.this; 또한 버릴 수도 있습니다. – Kammaar

+0

이것은 Context 객체입니다 – Kammaar

14

는 자산 루트의 내용을 표시합니다 귀하의 경우

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

위의 코드에서 작동이보십시오. 예를 들어

... 자산 구조로 아래 ..

assets 
|__Dir1 
|__Dir2 
|__File1 

발췌문의 출력이 될 것입니다 .... 있는 Dir1 Dir2라는을 File1

당신의 내용이 필요한 경우 디렉터리 디렉터리

목록 기능에서 디렉터리의 이름을 전달하십시오.

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

Eclipse가 오류를 표시했기 때문에이 코드를 편집했습니다. 'String [] f = null; \t \t { \t \t \t f = getAssets(). list (""); \t \t} 캐치 (IOException이 전자) { \t \t \t // TODO) (catch 블록을 \t \t \t e.printStackTrace을 자동 생성; \t \t} \t (문자열 f1 : f) { \t Log.v ("names", f1); }}'to this –

+1

결과로 - 에뮬레이터가 마약, 이미지, 소리, 웹킷 –

+1

4 개 항목 목록을 보여줍니다. 예. 그렇지만 자산 내부에 youtlr 디렉토리의 이름을 전달하면 내부로 빠져 나갈 수 있습니다. 대괄호 상위 폴더와 모든 내용이 나열됩니다 ...... like getAssets(). list ("drugs"); – cafebabe1991

0

희망이 도움말을 호출 할 경우 : 모든 폴더를 복사하는 코드를 다음

를 그리고 내용과 하위 폴더의 내용입니다 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(); 
} 
관련 문제