Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6186

android: how can get full path of a file stored in a folder in sdcard?

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.

Upvotes: 1

Views: 32290

Answers (2)

brianestey
brianestey

Reputation: 8302

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?

Upvotes: 1

Egor
Egor

Reputation: 40193

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.

Upvotes: 6

Related Questions