【问题标题】:Open folder on button click with "My files" application使用“我的文件”应用程序单击按钮打开文件夹
【发布时间】:2012-08-23 07:46:03
【问题描述】:

我正在制作安卓应用程序。在按钮单击时,我只需要打开我有 pdf 的特定文件夹,以便用户可以选择一些 pdf 从该文件夹中读取。

我设法列出了 pdf 文件夹中的所有文件,但这不是我需要的。最合乎逻辑的解决方案是使用“我的文件”应用程序打开文件夹,该应用程序已经是 Android 操作系统的一部分。有没有什么乳清可以在安卓上做到这一点?

【问题讨论】:

  • 我只想提一下“我的文件”不是 Android 操作系统的一部分。它只是某些 ROM 的一部分。您不能假设“我的文件”在所有设备上都可用

标签: java android directory


【解决方案1】:

小心。并非所有 Android 设备都有“我的文件”应用程序。所以最好的方法是创建自己的列表/文件浏览器。

【讨论】:

    【解决方案2】:

    这是我列出文件的代码,如何在点击时打开 pdf 文件。

        public class AndroidListFilesActivity extends ListActivity {
    
        private List<String> fileList = new ArrayList<String>();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            File root = new File(Environment.getExternalStorageDirectory()
                    .getAbsolutePath());    
            File pdf = new File(root, "PDF");
            ListDir(pdf);
        }
    
        void ListDir(File f) {
            File[] files = f.listFiles();
            fileList.clear();
            for (File file : files) {
                fileList.add(file.getPath());
            }
    
            ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, fileList);
            setListAdapter(directoryList);
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      此代码在没有“我的文件”应用程序的情况下工作,这是更好的解决方案。

          public class AndroidListFilesActivity extends ListActivity {
      
          File root;
          File pdf;
      
          private List<String> fileList = new ArrayList<String>();
      
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              root = new File(Environment.getExternalStorageDirectory()
                      .getAbsolutePath());
              // ListDir(root);
      
              pdf = new File(root, "PDF");
              ListDir(pdf);
          }
      
          void ListDir(File f) {
              File[] files = f.listFiles();
              fileList.clear();
              for (File file : files) {
                  fileList.add(file.getPath());
              }
      
              ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,
                      android.R.layout.simple_list_item_1, fileList);
      
              setListAdapter(directoryList);
      
          }
      
          public void onListItemClick(ListView parent, View v, int position, long id) {
              //selection.setText(fileList.indexOf(simple_list_item_1));
              OpenPdf(fileList.get(position).toString());
          }
      
          public void OpenPdf(String path)
          {
                File file = new File(path);
                if (file.exists()) {
                    Uri p = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(p, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      
                    try {
                        startActivity(intent);
                    } 
                    catch (ActivityNotFoundException e){
                    }
                }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-26
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2018-11-19
        • 1970-01-01
        相关资源
        最近更新 更多