【问题标题】:How can I use Intent.ACTION_VIEW to view the contents of a folder?如何使用 Intent.ACTION_VIEW 查看文件夹的内容?
【发布时间】:2011-02-25 06:44:00
【问题描述】:

我正在尝试使用意图查看特定文件夹中的图像。我的代码如下:

Intent intent = new Intent();  
intent.setAction(Intent.ACTION_VIEW);  
Uri imgUri = Uri.parse("file://sdcard/download");  
intent.setDataAndType(imgUri, "image/*");  
startActivity(intent);  

但是,每次运行时,我都会在日志中收到以下消息:

02-25 00:40:16.271: 错误/(8359): 无法打开“/下载”
02-25 00:40:16.271: 错误/(8359): 无法打开'/download'

我做错了什么?

【问题讨论】:

  • 您可以访问 SDCard 吗?
  • 这与问题完全无关,但其他人看不到此问题的编辑/重新标记链接吗?我是否错过了为什么他们不在那里?
  • Mudassir,我确实可以访问 sdcard,我通过在其上创建一个简单的文本文件来测试它。
  • 您是否尝试在 URI:"file:///sdcard/download" 中添加额外的斜杠?
  • 我刚刚试了一下,走得更远了!除了现在我得到一个不同的错误!在我的 URI 中添加一个额外的斜杠会得到这个错误:02-25 07:37:29.106: ERROR/(10632): Not JPEG: /sdcard/download/ 甚至在 URI 的末尾添加一个通配符,所以它看起来像这个“file:///sdcard/download/*”不能解决它。

标签: android android-manifest android-intent sd-card


【解决方案1】:

我认为这比其他人建议的要简单得多。我相信路径是区分大小写的。在您的示例中,“file://sdcard/Download”应该可以工作。

【讨论】:

    【解决方案2】:

    试试这样的

    Intent intent = new Intent();
    intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 1);
    

    Intent intent = new Intent();
    intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 1);
    

    【讨论】:

      【解决方案3】:

      不久前我遇到了类似的问题,我有一个实例要求我让用户选择从预定位置选择照片。我目前正在使用的工作解决方案要求您在设备上已经有某种文件管理器。我使用的是“OI 文件管理器”。它可以从 Android/Play 市场免费下载。话虽如此,您可以尝试使用:

       //--Note: You can supply the full location of the folder but if you don't know it and
      //         if the folder location is on the sdcard, provide the following:
       File root = new File(Environment.getExternalStorageDirectory() + File.separator + "myFolder" + File.separator);
       Uri uri = Uri.fromFile(root);
      
       Intent intent = new Intent();
       intent.setAction(android.content.Intent.ACTION_VIEW);
       intent.setData(Uri.fromFile(root));
       startActivityForResult(intent, PIC_REQUEST);
      

      这将打开一个对话框,要求您选择一个选项来处理请求。当它这样做时,只需在选择“OI 文件管理器”选项之前选中底角的“默认使用...”框。这将设置它,以便每次启动意图时它都会自动打开您指定的位置,以便您可以查看内容,而无需每次启动意图时深入到文件夹位置。不得不依赖第三方应用程序并不好,但它可能是您的一个选择。

      【讨论】:

        【解决方案4】:
        Intent intent = new Intent();  
        intent.setAction(Intent.ACTION_GET_CONTENT);
        Uri imgUri = Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
        intent.setDataAndType(imgUri, "file/*");   
        startActivity(intent);
        

        为我工作..

        【讨论】:

          猜你喜欢
          • 2022-01-03
          • 2021-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-17
          • 1970-01-01
          • 2011-04-05
          相关资源
          最近更新 更多