0

I'm having a hard time passing files with spaces in their names into Twilio chat.

Forgetting about the fact that I'm using Twilio, and just concentrating that I'm trying to upload a file from within the Android phone. I have an image on my android phone which I found its path as /Internal storage/Download/4893079.jpg

Below is the full code:

public void testing() {
        Messages messagesObject = channel.getMessages();
        try {
            messagesObject.sendMessage(
                    Message.options()
                            .withMedia(new FileInputStream("/Internal%20storage/Download/4893079.jpg"), "image/jpeg")
                            .withMediaFileName("4893079.jpg")
                            .withMediaProgressListener(new ProgressListener() {
                                @Override
                                public void onStarted() {
                                    System.out.println("Upload started");
                                }

                                @Override
                                public void onProgress(long bytes) {
                                    System.out.println("Uploaded " + bytes + " bytes");
                                }

                                @Override
                                public void onCompleted(String mediaSid) {
                                    System.out.println("Upload completed");
                                }
                            }),
                    new CallbackListener<Message>() {
                        @Override
                        public void onSuccess(Message msg) {
                            System.out.println("Successfully sent MEDIA message");
                        }

                        @Override
                        public void onError(ErrorInfo error) {
                            System.out.println("Error sending MEDIA message");
                        }
                    });
        } catch (FileNotFoundException e) {
            System.out.println("FAILED HORRIBLY");
            System.out.println(e);
            e.printStackTrace();
        }
    }

However running the app I end up with the error,

java.io.FileNotFoundException: /Internal%20storage/Download/4893079.jpg (No such file or directory)

As shown I've tried replacing space with %20, but that didn't work. I've also tried \ , \\ . However non of them worked. I've looked at Opening a local Android File with spaces, Save file in Android with spaces in file name.

How should I be replacing the space?

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Mark
  • 3,138
  • 5
  • 19
  • 36
  • "I found its path as /Internal storage/Download/4893079.jpg" are you sure about this? – njzk2 Aug 22 '21 at 16:34
  • The path you see is not always the path to use in your app. I recommend to create a short file selector app and print the uri to receive the path from an apps perspective. – Jakob Aug 22 '21 at 16:44

1 Answers1

0

You need to get the actual path programmatically. So the real path is

Environment.getExternalStorageDirectory().toString() + "/Download/4893079.jpg"

Internal storage is just something the user sees, but not a real path on the machine.

Jakob
  • 1,858
  • 2
  • 15
  • 26