2016-10-19 15 views
0

特定のファイルを削除するにはストレージアクセスフレームワークを使用しています。私はすでに、ユーザーが削除したい特定のファイルを知っています。この特定のファイルをファイルピッカーに表示する方法はありますか?現在は、常にルートディレクトリから開き、ユーザーはファイルに移動する必要があります。これはユーザビリティの問題です。ストレージアクセスフレームワーク - ACTION_OPEN_DOCUMENTファイルピッカーで特定のファイルを表示

または、希望の許可をリクエストする方法はありますか?

+0

ファイルブラウザで、削除するファイルを直接開きたいのですか? – vidulaJ

+0

はい、可能です。あるいは、それは既に選択されたファイルに直接ポップアップするだけです。 – Jon

+0

ファイルピッカー自体が必要ですか、ファイルダイアログが大丈夫ですか? – vidulaJ

答えて

0

これは私のために働いた。

package com.test.filedialog.file; 

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

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Environment; 
import android.util.Log; 
import com.test.filedialog.file.ListenerList.FireHandler; 

public class FileDialog { 
    private static final String PARENT_DIR = ".."; 
    private final String TAG = getClass().getName(); 
    private String[] fileList; 
    private File currentPath; 

    public interface FileSelectedListener { 
     void fileSelected(File file); 
    } 

    public interface DirectorySelectedListener { 
     void directorySelected(File directory); 
    } 

    private ListenerList<FileSelectedListener> fileListenerList = new ListenerList<FileSelectedListener>(); 
    private ListenerList<DirectorySelectedListener> dirListenerList = new ListenerList<DirectorySelectedListener>(); 
    private final Activity activity; 
    private boolean selectDirectoryOption; 
    private String fileEndsWith; 
    private String[] fileEndsWiths; 

    /** 
    * @param activity 
    * @param initialPath - HERE IS WHERE YOU SHOULD GIVE THE DIRECTORY YOU WANT 
    */ 
    public FileDialog(Activity activity, File initialPath) { 
     this.activity = activity; 
     if (!path.exists()) path = Environment.getExternalStorageDirectory(); 
     loadFileList(path); 
    } 

    /** 
    * @return file dialog 
    */ 
    public Dialog createFileDialog() { 
     Dialog dialog = null; 
     AlertDialog.Builder builder = new AlertDialog.Builder(activity); 

     builder.setTitle(currentPath.getPath()); 
     if (selectDirectoryOption) { 
      builder.setPositiveButton("Select directory", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Log.d(TAG, currentPath.getPath()); 
        fireDirectorySelectedEvent(currentPath); 
       } 
      }); 
     } 

     builder.setItems(fileList, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       String fileChosen = fileList[which]; 
       File chosenFile = getChosenFile(fileChosen); 
       if (chosenFile.isDirectory()) { 
        loadFileList(chosenFile); 
        dialog.cancel(); 
        dialog.dismiss(); 
        showDialog(); 
       } else fireFileSelectedEvent(chosenFile); 
      } 
     }); 

     dialog = builder.show(); 
     return dialog; 
    }  

    public void addFileListener(FileSelectedListener listener) { 
     fileListenerList.add(listener); 
    } 

    public void removeFileListener(FileSelectedListener listener) { 
     fileListenerList.remove(listener); 
    } 

    public void setSelectDirectoryOption(boolean selectDirectoryOption) { 
     this.selectDirectoryOption = selectDirectoryOption; 
    } 

    public void addDirectoryListener(DirectorySelectedListener listener) { 
     dirListenerList.add(listener); 
    } 

    public void removeDirectoryListener(DirectorySelectedListener listener) { 
     dirListenerList.remove(listener); 
    } 

    /** 
    * Show file dialog 
    */ 
    public void showDialog() { 
     createFileDialog().show(); 
    } 

    private void fireFileSelectedEvent(final File file) { 
     fileListenerList.fireEvent(new FireHandler<FileSelectedListener>() { 
      public void fireEvent(FileSelectedListener listener) { 
       listener.fileSelected(file); 
      } 
     }); 
    } 

    private void fireDirectorySelectedEvent(final File directory) { 
     dirListenerList.fireEvent(new FireHandler<DirectorySelectedListener>() { 
      public void fireEvent(DirectorySelectedListener listener) { 
       listener.directorySelected(directory); 
      } 
     }); 
    } 

    private void loadFileList(File path) { 
     this.currentPath = path; 
     List<String> r = new ArrayList<String>(); 
     if (path.exists()) { 
      if (path.getParentFile() != null) r.add(PARENT_DIR); 
      @SuppressWarnings("unused") 
      FilenameFilter filter = new FilenameFilter() { 
       public boolean accept(File dir, String filename) { 
        File sel = new File(dir, filename); 

        if (!sel.canRead()) return false; 
        if (selectDirectoryOption) return sel.isDirectory(); 
        else { 
         boolean endsWith = false; 
         if(fileEndsWiths != null){ 
          System.out.println("File Input Lengths : " + fileEndsWiths.length); 
          for(int i = 0; i < fileEndsWiths.length; i++){ 
           endsWith = fileEndsWiths[i] != null ? filename.toLowerCase(Locale.getDefault()).endsWith(fileEndsWiths[i]) : true; 
          } 
         } 
         else{ 
          endsWith = fileEndsWith != null ? filename.toLowerCase(Locale.getDefault()).endsWith(fileEndsWith) : true; 
          System.out.println("No Files"); 
         }      

         return endsWith || sel.isDirectory(); 
        } 
       } 
      }; 
//   String[] fileList1 = path.list(filter); 
      File[] fileList1 = path.listFiles(); 
      for (File file : fileList1) { 
       r.add(file.getName()); 
      } 
     } 
     fileList = (String[]) r.toArray(new String[]{}); 
    } 

    private File getChosenFile(String fileChosen) { 
     if (fileChosen.equals(PARENT_DIR)) return currentPath.getParentFile(); 
     else return new File(currentPath, fileChosen); 
    } 

    public void setFileEndsWith(String[] fileEndsWith) { 
     this.fileEndsWiths = fileEndsWith; 
    } 

    public void setFileEndsWith(String fileEndsWith) { 
     this.fileEndsWith = fileEndsWith != null ? fileEndsWith.toLowerCase(Locale.getDefault()) : fileEndsWith; 
    } 
} 

class ListenerList<L> { 
    private List<L> listenerList = new ArrayList<L>(); 

    public interface FireHandler<L> { 
     void fireEvent(L listener); 
    } 

    public void add(L listener) { 
     listenerList.add(listener); 
    } 

    public void fireEvent(FireHandler<L> fireHandler) { 
     List<L> copy = new ArrayList<L>(listenerList); 
     for (L l : copy) { 
      fireHandler.fireEvent(l); 
     } 
    } 

    public void remove(L listener) { 
     listenerList.remove(listener); 
    } 

    public List<L> getListenerList() { 
     return listenerList; 
    } 
} 

選択したファイルを参照して受信するには、次のようにします。

private void browseFiles(){ 
    File mPath = new File(Environment.getExternalStorageDirectory() + "//DIR//"); 
    FileDialog fileDialog = new FileDialog(getActivity(), mPath); 
    /** 
    * Specify the file type here 
    * **/ 
    String[] fileTypes = new String[] { ".png", ".jpg", ".jpeg", ".PNG", ".JPG", ".JPEG", ".gif", ".GIF", ".tif", ".TIF"}; 
    //This fileTypes Array is optional 
    fileDialog.setFileEndsWith(fileTypes); 
    fileDialog.setFileEndsWith(".png"); 

    fileDialog.addFileListener(new FileDialog.FileSelectedListener() { 
     public void fileSelected(File file) { 
      //file is the selected File 
     } 
    }); 

    fileDialog.showDialog(); 
} 
+0

提案していただきありがとうございます。実際に、私がkitkat以上のファイルダイアログを使ってファイルを削除しようとすると、まだパーミッションエラーが出ます。私は、ストレージアクセスフレームワークを使用する必要があると思います。 https://developer.android.com/guide/topics/providers/document-provider.html – Jon

+0

このソリューションには、Permissionモデルを実際に組み込むことができます。私はこれをパーミッションモデルと、いくつかのアプリでうまく統合しました。 – vidulaJ

関連する問題