机器人:如何获取存储在SD卡的文件夹中的文件的完整路径? [英] android: how can get full path of a file stored in a folder in sdcard?

查看:375
本文介绍了机器人:如何获取存储在SD卡的文件夹中的文件的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的机器人。我想获得由用户选择的文件的完整路径。我的文件
存储在SD卡。但也可以是SD卡的文件夹中。

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.

我在SD卡的一些文件夹。我想关于这一点我点击一个文件的完整路径。

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

假设我有 peacock.png 文件夹中的图像/鸟的形象。

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

所以,路径是 MNT / SD卡/图像/只/ peacock.png

请建议我我怎样才能得到一个文件的完整路径。

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

如果你需要,我现在用的帮助我的code然后告诉我,我会在这里发出。

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

感谢你在前进。

推荐答案

下面是从这是一个code段教程,这显示了选秀文件意图的实现:

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");
   }
}

正如你所看到的,你的 onActivityResult()方法返回的意图,其中包含的文件路径,可以使用 intent.getData()法来提取。然后你只需创建一个使用该路径的File对象,并使用 file.getAbsolutePath()方法得到它的绝对路径。希望这有助于。

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.

这篇关于机器人:如何获取存储在SD卡的文件夹中的文件的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆