2017-03-04 5 views
-8

내부 저장 장치 또는 카드에서 파일 선택기를 만들고 싶습니다. 많은 코드를 시도했지만 내 요구를 충족시키지 못했습니다. 누군가 소스 코드 또는 예제를 보내주십시오. 미리 감사드립니다 !!!!! 여기 내 codes- 버튼으로 파일 열기 만들기 android

FilePick_MainActivity.java 

    public class FilePick_MainActivity extends Activity implements OnClickListener { 

    private static final int REQUEST_PICK_FILE = 1; 

    private TextView filePath; 
    private Button Browse; 
    private File selectedFile; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_file_pick__main); 

    filePath = (TextView)findViewById(R.id.file_path); 
    Browse = (Button)findViewById(R.id.browse); 
    Browse.setOnClickListener(this); 
    } 

    public void onClick(View v) { 

    switch(v.getId()) { 

     case R.id.browse: 
      Intent intent = new Intent(this, FilePicker.class); 
      startActivityForResult(intent, REQUEST_PICK_FILE); 
      break; 
    } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if(resultCode == RESULT_OK) { 

     switch(requestCode) { 

      case REQUEST_PICK_FILE: 

       if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) { 

        selectedFile = new File 
          (data.getStringExtra(FilePicker.EXTRA_FILE_PATH)); 
        filePath.setText(selectedFile.getPath()); 
       } 
       break; 
      } 
     } 
    } 

    FilePicker.java 

    package com.example.rama.filepick; 

    public class FilePicker extends ListActivity { 

    public final static String EXTRA_FILE_PATH = "file_path"; 
    public final static String EXTRA_SHOW_HIDDEN_FILES = "show_hidden_files"; 
    public final static String EXTRA_ACCEPTED_FILE_EXTENSIONS = "accepted_file_extensions"; 
    private final static String DEFAULT_INITIAL_DIRECTORY = "/"; 

    protected File Directory; 
    protected ArrayList<File> Files; 
    protected FilePickerListAdapter Adapter; 
    protected boolean ShowHiddenFiles = false; 
    protected String[] acceptedFileExtensions; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LayoutInflater inflator = (LayoutInflater) 
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View emptyView = inflator.inflate(R.layout.empty_view, null); 
    ((ViewGroup) getListView().getParent()).addView(emptyView); 
    getListView().setEmptyView(emptyView); 

    Directory = new File(DEFAULT_INITIAL_DIRECTORY); 

    Files = new ArrayList<File>(); 

    Adapter = new FilePickerListAdapter(this, Files); 
    setListAdapter(Adapter); 

    acceptedFileExtensions = new String[] {}; 

    if(getIntent().hasExtra(EXTRA_FILE_PATH)) 
     Directory = new File(getIntent().getStringExtra(EXTRA_FILE_PATH)); 

     if(getIntent().hasExtra(EXTRA_SHOW_HIDDEN_FILES)) 
     ShowHiddenFiles = getIntent().getBooleanExtra(EXTRA_SHOW_HIDDEN_FILES, false); 

     if(getIntent().hasExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS)) { 

     ArrayList<String> collection = 
       getIntent().getStringArrayListExtra(EXTRA_ACCEPTED_FILE_EXTENSIONS); 

     acceptedFileExtensions = (String[]) 
       collection.toArray(new String[collection.size()]); 
     } 
    } 

    @Override 
    protected void onResume() { 
    refreshFilesList(); 
    super.onResume(); 
    } 

    protected void refreshFilesList() { 

    Files.clear(); 
    ExtensionFilenameFilter filter = 
      new ExtensionFilenameFilter(acceptedFileExtensions); 

    File[] files = Directory.listFiles(filter); 

    if(files != null && files.length > 0) { 

     for(File f : files) { 

      if(f.isHidden() && !ShowHiddenFiles) { 

       continue; 
      } 

      Files.add(f); 
     } 

     Collections.sort(Files, new FileComparator()); 
     } 

    Adapter.notifyDataSetChanged(); 
    } 

    @Override 
    public void onBackPressed() { 

    if(Directory.getParentFile() != null) { 

     Directory = Directory.getParentFile(); 
     refreshFilesList(); 
     return; 
    } 

     super.onBackPressed(); 
    } 

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

     File newFile = (File)l.getItemAtPosition(position); 

     if(newFile.isFile()) { 

     Intent extra = new Intent(); 
     extra.putExtra(EXTRA_FILE_PATH, newFile.getAbsolutePath()); 
     setResult(RESULT_OK, extra); 
     finish(); 
     } 
     else { 

     Directory = newFile; 
     refreshFilesList(); 
     } 

     super.onListItemClick(l, v, position, id); 
    } 

    private class FilePickerListAdapter extends ArrayAdapter<File> { 

    private List<File> mObjects; 

    public FilePickerListAdapter(Context context, List<File> objects) { 

     super(context, R.layout.list_item, android.R.id.text1, objects); 
     mObjects = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row = null; 

     if(convertView == null) { 

      LayoutInflater inflater = (LayoutInflater) 
        getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      row = inflater.inflate(R.layout.list_item, parent, false); 
     } 
     else 
      row = convertView; 

     File object = mObjects.get(position); 

     ImageView imageView = (ImageView)row.findViewById(R.id.file_picker_image); 
     TextView textView = (TextView)row.findViewById(R.id.file_picker_text); 
     textView.setSingleLine(true); 
     textView.setText(object.getName()); 

      if(object.isFile()) 
      imageView.setImageResource(R.drawable.file); 

     else 
      imageView.setImageResource(R.drawable.folder); 

     return row; 
     } 
    } 

    private class FileComparator implements Comparator<File> { 

     public int compare(File f1, File f2) { 

     if(f1 == f2) 
      return 0; 

     if(f1.isDirectory() && f2.isFile()) 
      return -1; 

     if(f1.isFile() && f2.isDirectory()) 
      return 1; 

     return f1.getName().compareToIgnoreCase(f2.getName()); 
     } 
    } 

    private class ExtensionFilenameFilter implements FilenameFilter { 

    private String[] Extensions; 

    public ExtensionFilenameFilter(String[] extensions) { 

     super(); 
     Extensions = extensions; 
    } 

    public boolean accept(File dir, String filename) { 

     if(new File(dir, filename).isDirectory()) { 

      return true; 
     } 

     if(Extensions != null && Extensions.length > 0) { 

      for(int i = 0; i < Extensions.length; i++) { 

       if(filename.endsWith(Extensions[i])) { 

        return true; 
       } 
      } 
      return false; 
     } 
     return true; 
    } 
} 

Android manifest 
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="rama.Filebrowse" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
    android:minSdkVersion="5" 
    android:targetSdkVersion="17" /> 

    <application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.rama.filepick.FilePicker" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity android:name="com.example.rama.filepick.MainActivity"> </activity> 

    </application> 

</manifest> 

나는 단지 안드로이드 시스템 폴더 및 파일이 아닌 내부 스토리지 및 SDCARD을 보여주는이 코드를 실행

.

+4

책, 도구, 소프트웨어 라이브러리, 자습서 또는 기타 오프 사이트 리소스를 추천하거나 찾도록 요청하는 질문은 오타가있는 답변 및 스팸을 유치하는 경향이 있으므로 스택 오버플로에 대한 주제와 관련이 없습니다. 대신 문제를 설명하고 지금까지 해결 된 문제를 설명하십시오. –

+0

무엇을 시도 했습니까? 어떤 오류가 발생합니까? 조금 더 구체적으로하십시오 –

+0

이 코드를 확인하십시오 #Matheus – Jack

답변

1


"Android-Multipicker-Library"에서는 파일을 선택할 수 있습니다. 이 도서관을 살펴보십시오

+0

Ok #rahul 나는 노력하고 있습니다 !!! – Jack