使用按钮android创建文件开启工具

时间:2017-03-04 04:34:22

标签: java 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>

当我运行此代码时,它只显示android系统文件夹和文件,而不是内部存储和SD卡。

1 个答案:

答案 0 :(得分:1)


"Android-Multipicker-Library"允许您选择文件。看看这个图书馆