【问题标题】:Open File Dialog in Android?在Android中打开文件对话框?
【发布时间】:2011-08-02 09:23:54
【问题描述】:

我可以在 Android 中使用 OpenFileDialog 吗?我想从 OpenFileDialog 中选择图像并将图像保存到 SQLite 数据库和资源文件夹。我该怎么做?

【问题讨论】:

    标签: android openfiledialog


    【解决方案1】:

    这个link帮助了我

    我创建了FolderLayout,这可能会对您有所帮助。

    文件夹视图.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView android:id="@+id/path" android:text="Path"
            android:layout_width="match_parent" android:layout_height="wrap_content"></TextView>
        <ListView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/list"></ListView>
    
    </LinearLayout>
    

    文件夹布局.java

    package com.testsample.activity;
    
    
    
    
       public class FolderLayout extends LinearLayout implements OnItemClickListener {
    
        Context context;
        IFolderItemListener folderListener;
        private List<String> item = null;
        private List<String> path = null;
        private String root = "/";
        private TextView myPath;
        private ListView lstView;
    
        public FolderLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            // TODO Auto-generated constructor stub
            this.context = context;
    
    
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = layoutInflater.inflate(R.layout.folderview, this);
    
            myPath = (TextView) findViewById(R.id.path);
            lstView = (ListView) findViewById(R.id.list);
    
            Log.i("FolderView", "Constructed");
            getDir(root, lstView);
    
        }
    
        public void setIFolderItemListener(IFolderItemListener folderItemListener) {
            this.folderListener = folderItemListener;
        }
    
        //Set Directory for view at anytime
        public void setDir(String dirPath){
            getDir(dirPath, lstView);
        }
    
    
        private void getDir(String dirPath, ListView v) {
    
            myPath.setText("Location: " + dirPath);
            item = new ArrayList<String>();
            path = new ArrayList<String>();
            File f = new File(dirPath);
            File[] files = f.listFiles();
    
            if (!dirPath.equals(root)) {
    
                item.add(root);
                path.add(root);
                item.add("../");
                path.add(f.getParent());
    
            }
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                path.add(file.getPath());
                if (file.isDirectory())
                    item.add(file.getName() + "/");
                else
                    item.add(file.getName());
    
            }
    
            Log.i("Folders", files.length + "");
    
            setItemList(item);
    
        }
    
        //can manually set Item to display, if u want
        public void setItemList(List<String> item){
            ArrayAdapter<String> fileList = new ArrayAdapter<String>(context,
                    R.layout.row, item);
    
            lstView.setAdapter(fileList);
            lstView.setOnItemClickListener(this);
        }
    
    
        public void onListItemClick(ListView l, View v, int position, long id) {
            File file = new File(path.get(position));
            if (file.isDirectory()) {
                if (file.canRead())
                    getDir(path.get(position), l);
                else {
    //what to do when folder is unreadable
                    if (folderListener != null) {
                        folderListener.OnCannotFileRead(file);
    
                    }
    
                }
            } else {
    
    //what to do when file is clicked
    //You can add more,like checking extension,and performing separate actions
                if (folderListener != null) {
                    folderListener.OnFileClicked(file);
                }
    
            }
        }
    
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            onListItemClick((ListView) arg0, arg0, arg2, arg3);
        }
    
    }
    

    还有一个接口IFolderItemListener,用于添加在单击fileItem 时要执行的操作

    IFolderItemListener.java

    public interface IFolderItemListener {
    
        void OnCannotFileRead(File file);//implement what to do folder is Unreadable
        void OnFileClicked(File file);//What to do When a file is clicked
    }
    

    也是定义行的xml

    row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rowtext" android:layout_width="fill_parent"
        android:textSize="23sp" android:layout_height="match_parent"/>
    

    如何在您的应用程序中使用

    在你的xml中,

    文件夹.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="horizontal" android:weightSum="1">
        <com.testsample.activity.FolderLayout android:layout_height="match_parent" layout="@layout/folderview"
            android:layout_weight="0.35"
            android:layout_width="200dp" android:id="@+id/localfolders"></com.testsample.activity.FolderLayout></LinearLayout>
    

    在您的活动中,

    SampleFolderActivity.java

    public class SampleFolderActivity extends Activity implements IFolderItemListener {
    
        FolderLayout localFolders;
    
        /** Called when the activity is first created. */
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            localFolders = (FolderLayout)findViewById(R.id.localfolders);
            localFolders.setIFolderItemListener(this);
                localFolders.setDir("./sys");//change directory if u want,default is root   
    
        }
    
        //Your stuff here for Cannot open Folder
        public void OnCannotFileRead(File file) {
            // TODO Auto-generated method stub
            new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle(
                    "[" + file.getName()
                            + "] folder can't be read!")
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
    
                        public void onClick(DialogInterface dialog,
                                int which) {
    
    
                        }
                    }).show();
    
        }
    
    
        //Your stuff here for file Click
        public void OnFileClicked(File file) {
            // TODO Auto-generated method stub
            new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("[" + file.getName() + "]")
            .setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int which) {
    
    
                        }
    
                    }).show();
        }
    
    }
    

    导入所需的库。希望这些对您有所帮助...

    【讨论】:

      【解决方案2】:

      查看Android File Dialog

      我希望你知道如何下载源代码...你可能需要像tortoise这样的程序

      【讨论】:

        【解决方案3】:

        OpenFileDialog 是我编写的自定义对话框。你几乎只需要打电话

        new OpenFileDialog().show(); 
        

        得到你想要的。

        【讨论】:

          猜你喜欢
          • 2010-12-11
          • 1970-01-01
          • 1970-01-01
          • 2013-05-12
          • 2022-01-23
          • 2011-02-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多