android:如何获取存储在SD卡文件夹中的文件的完整路径?

发布于 2024-12-09 09:09:10 字数 310 浏览 1 评论 0原文

我在安卓工作。我想获取用户选择的文件的完整路径。我的文件
都存储在sd卡中。但可能位于 SD 卡的文件夹中。

我的 SD 卡中有一些文件夹。我想获取我单击的文件的完整路径。

假设我在 image/birds 文件夹中有一个图像 peacock.png

所以路径是 mnt/sdcard/image/birds/peacock.png

请建议我如何获取文件的完整路径。

如果您需要我正在使用的代码来提供帮助,请告诉我,我会将其发送到这里。

先感谢您。

I am working in android. i want to get the full path of a file selected by user. my files
are stored in sdcard. but may be in a folder in sd card.

I have some folders in my sdcard. I want to get the full path of a file on which i click.

Suppose I have an image peacock.png in folder image/birds.

So the path is mnt/sdcard/image/birds/peacock.png

Please suggest me how can i get the full path of a file.

If you need my code which i am using for help then tell me i will send it here.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-12-16 09:09:10

这是教程中的代码片段,它显示了选择文件 Intent 实现:

    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
   if (requestCode == PICK_REQUEST_CODE)
   {
   if (resultCode == RESULT_OK)
   {
      Uri uri = intent.getData();
      String type = intent.getType();
      LogHelper.i(TAG,"Pick completed: "+ uri + " "+type);
      if (uri != null)
      {
         String path = uri.toString();
         if (path.toLowerCase().startsWith("file://"))
         {
            // Selected file/directory path is below
            path = (new File(URI.create(path))).getAbsolutePath();
         }

      }
   }
   else LogHelper.i(TAG,"Back from pick with cancel status");
   }
}

如您所见,您的onActivityResult() 方法返回 Intent,其中包含文件路径,可以使用以下命令提取该文件路径intent.getData() 方法。然后,您只需使用此路径创建一个 File 对象,并使用 file.getAbsolutePath() 方法获取它的绝对路径。希望这有帮助。

Here's a code snippet from this tutorial, that shows the pick file Intent implementation:

    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
   if (requestCode == PICK_REQUEST_CODE)
   {
   if (resultCode == RESULT_OK)
   {
      Uri uri = intent.getData();
      String type = intent.getType();
      LogHelper.i(TAG,"Pick completed: "+ uri + " "+type);
      if (uri != null)
      {
         String path = uri.toString();
         if (path.toLowerCase().startsWith("file://"))
         {
            // Selected file/directory path is below
            path = (new File(URI.create(path))).getAbsolutePath();
         }

      }
   }
   else LogHelper.i(TAG,"Back from pick with cancel status");
   }
}

As you can see, your onActivityResult() method returns you the Intent, which contains the file path, that can be extracted using intent.getData() method. Then you just create a File object using this path, and get the absolute path of it using file.getAbsolutePath() method. Hope this helps.

可爱暴击 2024-12-16 09:09:10

如果您的意思是在 onFileClick 中,则会传递一个选项。您的 Option 类似乎包含完整路径,因为它被传递到构造函数中,例如:

new Option(ff.getName(),"Folder",ff.getAbsolutePath())

您不能以某种方式获取该属性吗?

If you mean in the onFileClick, it is passed an Option. Your Option class seems to contain the full path, as it is passed into the constructor, for example:

new Option(ff.getName(),"Folder",ff.getAbsolutePath())

Can't you get at that property somehow?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文