1

I'm working on an app (and yes, I'm a beginner to app development, so if you have any suggestions to use in place of the code I'm posting, then please say so). I'm trying to load an audio file to be processed later in the use of the app. but what I'm stuck with at the moment is the following. When you click on "add" it takes you to the file explorer where to need to select an audio file. Once the file is selected, the file name needs to display in a TextView. The problem I'm having is that the file name isn't being displayed correctly.

test filename: Test audio file.mp3

result in the TextView: primary%3ADownload%2FTest%20audio%20file.mp3

So its setting the entire path as the TextView with % signs etc. and I don't know why. How can I make it display only the file name correctly without the full path?

Code for opening file explorer:

Intent explorer = new Intent(Intent.ACTION_GET_CONTENT, MediaStore.Audio.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(explorer, SELECT_AUDIO_FILE);

Code for retrieving the filename and setting it to the TextView:

audioUri = data.getData();
            File fileToProcess = new File("" + audioUri);
            String audioFileName = fileToProcess.getName();

            fileNameDisplay.setText(audioFileName);

xTwisteDx
  • 2,152
  • 1
  • 9
  • 25
Emelio
  • 37
  • 7
  • 3
    `ACTION_GET_CONTENT` does not return a filesystem path, and a `Uri` is not something that you pass to the `File` constructor. See https://stackoverflow.com/q/49221312/115145 and https://stackoverflow.com/q/48510584/115145 and https://stackoverflow.com/q/59123162/115145 for more. – CommonsWare Dec 27 '19 at 14:27
  • @CommonsWare Thanks for that. I feel like I'm missing something though because I'm not at a solution yet. Someone answered but that answer has been removed. it must be wrong because it doesn't solve my problem. However, the result of that code seems better. the problem with it is that the TextView displays the full path of the file instead of the file name only. I dont seem to be able to make it display only the filename. – Emelio Dec 27 '19 at 15:41
  • There is no filename. For example, `https://stackoverflow.com/questions/59502063/file-name-is-not-displaying-correctly-in-the-textview-in-android-studio?noredirect=1#comment105178210_59502063` is a `Uri`. What is the filename? You are welcome to take the `Uri`, pass that to `DocumentFile.fromSingleUri()`, and try calling `getName()` on the `DocumentFile` that you get back. That's not guaranteed to work for `ACTION_GET_CONTENT` (it's designed for `ACTION_OPEN_DOCUMENT`), but if you get a `Uri` whose scheme is `content`, there is a good chance that it will work. – CommonsWare Dec 27 '19 at 15:54
  • But you need to understand that neither `ACTION_GET_CONTENT` nor `ACTION_OPEN_DOCUMENT` have much to do with files. The sooner you stop thinking in terms of files, the more success you will have when using those `Intent` actions. I have a long series of blog posts [starting with this one](https://commonsware.com/blog/2019/10/19/scoped-storage-stories-saf-basics.html) on using `ACTION_OPEN_DOCUMENT` and the rest of the Storage Access Framework. – CommonsWare Dec 27 '19 at 15:56
  • Thanks a lot for the info. I will have to do some more research on this topic and will definitely be looking into your blog posts for more info. – Emelio Dec 27 '19 at 16:01

2 Answers2

0

You could try something like this. The %2F is a URL encoding for a forward slash '/' which is part of the path to your file. By taking the last element in the array after the split, you should just be getting the 'Test%20audio%20file.mp3' string. Then, you need to replace the encoded ' ' characters with a real ' ' character.

/* String to split. */
String stringToSplit = "primary%3ADownload%2FTest%20audio%20file.mp3";
String[] tempArray;

/* delimiter */
String delimiter = "%2F";

/* given string will be split by the argument delimiter provided. */
tempArray = stringToSplit.split(delimiter);

String result = tempArray[tempArray.length - 1];
result = result.replace("%20", " ");

fileNameDisplay.setText(result);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • This makes unfounded assumptions about the nature of the `Uri` being returned by `ACTION_GET_CONTENT`. It so happens that on this device and this version of Android and this particular document provider and this particular document, a filename-like thing is mangled into the last path segment of the `Uri`. That is not a requirement. – CommonsWare Dec 27 '19 at 17:07
  • I think you just solved my problem. thanks a lot, man. I just needed to edit it slightly and add 2 more strings to be replaced. thanks again! It's now displaying only the file name. I doubt any file will contain the characters that are being removed/replaced. Also I understand that it may possibly affect the display of the filename, but for the moment, this is the result I need – Emelio Dec 27 '19 at 17:25
  • Yea, it's good not to over-think things too much! Good luck! – Michael Dougan Dec 27 '19 at 18:29
0

I spent some time searching and reading and ended up with the below code, which displays the file name only. It's working well on all emulators and physical devices I tested it on.

Uri returnUri = data.getData();
            Cursor returnCursor =
                    getContentResolver().query(returnUri, null, null, null, null);

            int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);

            returnCursor.moveToFirst();

            fileNameDisplay.setText(returnCursor.getString(nameIndex));
Emelio
  • 37
  • 7