2013-06-18 2 views
1

나는 간단한 파일 관리자가 있습니다. 디렉토리에서 파일을 탐색하는 동안 .txt 및 .pdf와 같은 특정 파일 확장명 만 표시됩니다. 디렉토리도 표시됩니다. 내가 어떻게 해. 다음 코드를 추가하거나 수정해야하는 항목. FileManager.java는 IS-간단한 파일 관리자를위한 필터 파일 확장자

package com.radiobot.speedreaderv1_1; 


import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 



import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.DialogInterface.OnClickListener; 

import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class FileManager extends ListActivity { 

     private enum DISPLAYMODE{ ABSOLUTE, RELATIVE; } 

     private final DISPLAYMODE displayMode = DISPLAYMODE.ABSOLUTE; 
     private List<String> directoryEntries = new ArrayList<String>(); 
     private File currentDirectory = new File("/"); 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle icicle) { 
       super.onCreate(icicle); 
       browseToRoot(); 
     } 


     private void browseToRoot() { 
       browseTo(new File("/")); 
    } 


     private void upOneLevel(){ 
       if(this.currentDirectory.getParent() != null) 
         this.browseTo(this.currentDirectory.getParentFile()); 
     } 

     private void browseTo(final File aDirectory){ 
       if (aDirectory.isDirectory()){ 
         this.currentDirectory = aDirectory; 
         fill(aDirectory.listFiles()); 
       } 
        else{ 
         OnClickListener okButtonListener = new OnClickListener(){ 
           // @Override 
           public void onClick(DialogInterface arg0, int arg1) { 
             { 


              try 
              { 
              Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW); 

              File file = new File(aDirectory.getAbsolutePath()); 
              String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); 

              String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
              myIntent.setDataAndType(Uri.fromFile(file),mimetype); 
              startActivity(myIntent); 
              } 
              catch (Exception e) 
              { 
              e.getMessage(); 
              } 

             } 
           } 
         }; 
         OnClickListener cancelButtonListener = new OnClickListener(){ 
           // @Override 
           public void onClick(DialogInterface arg0, int arg1) { 
             // Do nothing 
           } 
         }; 

         new AlertDialog.Builder(this) 
         .setIcon(R.drawable.ic_launcher) 
         .setTitle(
           "Do you want to open this file" + aDirectory.getName()) 
         .setPositiveButton("OK", okButtonListener) 
         .setNegativeButton("Cancel", cancelButtonListener) 
                   .show(); 
       } 
     } 

     private void fill(File[] files) { 
       this.directoryEntries.clear(); 

       try { 
         Thread.sleep(10); 
       } catch (InterruptedException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
       } 
       this.directoryEntries.add(""); 

       if(this.currentDirectory.getParent() != null) 
         this.directoryEntries.add(".."); 

       switch(this.displayMode){ 
         case ABSOLUTE: 
           for (File file : files){ 
             this.directoryEntries.add(file.getPath()); 
           } 
           break; 
         case RELATIVE: 
           int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length(); 
           for (File file : files){ 
             this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght)); 
           } 
           break; 
       } 

       ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, 
           R.layout.filemanager, this.directoryEntries); 

       this.setListAdapter(directoryList); 
     } 

     @Override 
     protected void onListItemClick(ListView l, View v, int position, long id) { 

      String position2 = (String) l.getItemAtPosition(position); 

     String selectedFileString = position2; 

       if (selectedFileString.equals(".")) { 
         // Refresh 
         this.browseTo(this.currentDirectory); 
       } else if(selectedFileString.equals("..")){ 
         this.upOneLevel(); 
       } else { 
         File clickedFile = null; 
         switch(this.displayMode){ 
           case RELATIVE: 

            clickedFile = new File(position2); 
             break; 
           case ABSOLUTE: 

            clickedFile = new File(position2);  
            break; 
         } 
         if(clickedFile != null) 

           this.browseTo(clickedFile); 
       } 
     } 


} 
+0

http://stackoverflow.com/questions/15853611/how-can-i-add-sdcard-images-to-coverflow/15853710#15853710이 도움이 될 수 있습니다 확인. 사용할 수있는 with.png로 끝나는 파일 필터링 | txt와 일치시키는 연산자 | pdf .. – Raghunandan

답변

0

This may be useful.

의 FileFilter 필터 = 새로운 FileNameExtensionFilter ("JPEG 파일", "JPG" "JPEG")을 파일; JFileChooser fileChooser = ...; fileChooser.addChoosableFileFilter (filter);

+1

이것은 안드로이드입니다. JFileChoosr는 java 용입니다. – Raghunandan

+0

위의 코드를 어디에 두어야할까요? –

+0

http://andackoverflow.com/questions/10934099/android-and-jfilechooser ... 나는 그것이 사용될 수 있다고 확신한다. 나는 지식의 범위를 벗어나 내가 모르는 것을 말하고 싶지 않습니다. 여기 다소 오보가되어 사과 드리지만 관련 정보로 남겨 두겠습니다. – jdero

0

browseTo()에서 당신은 목록에 디렉토리의 모든 파일을 추가 :

fill(aDirectory.listFiles()); // here's the problem 

File.listFiles(FileFilter filter)에서 봐,이 지정된 필터에 부합 파일 만 반환합니다.

File[] files = aDirectory.listFiles(new FileFilter() { 
    @Override 
    boolean accept(File pathname) { 
     // return true if and only if pathname has the desired extension 
     if (pathname.getName().endsWith(".txt") || pathname.isDirectory()) 
      return true; // accept only .txt files and directories 
     else 
      return false; 
    } 
}); 
+0

경로 이름으로 대체해야하는 항목은 무엇입니까? 어떻게 써야합니까? –

+0

accept 메서드에서 경로 이름을 처리하여 논리를 작성하고 true 또는 false를 반환합니다. –

+0

내가 파일 [] 파일 =의 aDirectory.listFiles을 추가 한 (새의 FileFilter() { @Override \t \t \t \t \t \t \t 공공 부울 동의 (파일 경로) { 경우 (pathname.getName(). endsWith (".txt")) true를 반환; else false를 반환; } }); 하지만 작동하지 않습니다. 그 모든 파일을 보여줍니다. –