0

I'm working on my android app and i have a problem. I need to get the file path for. Below you can see my code

private void pickStorageForMap()
    {


        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("application/pdf");

        startActivityForResult(intent, PICK_MAP_GPX);
    }


protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

       if (requestCode == PICK_MAP_GPX && resultCode == RESULT_OK) {
            if(data.getData() != null) {

                Uri uri = data.getData();
                String path = uri.getPath();



                Toast.makeText(mContext, "Type "+ path, Toast.LENGTH_SHORT).show();




            }else
            {
                Toast.makeText(mContext, "There is no file", Toast.LENGTH_LONG).show();
            }

        }
    }

The problem is that the file I select is not a Mime type, I need to select a file with the gpx extension. And to check if the file has the extension I need, I want to extract its name from the path. But the problem is that when I get the path, then instead of:

"/external/iamges/media/map.gpx"

I get

"/external/images/media/292021"

Can I somehow solve this problem so that instead of numbers at the end there is a file name?

lllll
  • 49
  • 2
  • Pass your `Uri` to `DocumentFile.fromSingleUri()`. You can then call `getName()` on the `DocumentFile` to get a "display name" for the content, and you can call `getType()` on the `DocumentFile` to get its MIME type. "I need to select a file with the gpx extension" -- there is no requirement for GPX data to be stored in something that uses a file extension. For example, this Web page contains HTML, and its `Uri` is `https://stackoverflow.com/questions/68301763/return-file-path-from-intent`, which does not have a file extension, let alone `html` as the file extension. – CommonsWare Jul 08 '21 at 12:39
  • Correct. And it looks like [the issue that I filed 5 years ago to add GPX support was ignored](https://issuetracker.google.com/issues/37120151). However, that does not change the fact that there is no requirement for GPX data to be in something that has a file extension. You are welcome to examine the "display name" and see if it happens to use a file extension. Personally, I would try parsing the content and handle the parsing exception. – CommonsWare Jul 08 '21 at 12:43

0 Answers0