I'm trying to access and download an image hosted on Google Cloud Storage with a signed URL, but it's throwing a MalformedURLException if I pass the signed URL string along as a parameter. I know it's a valid URL, as pasting it into my browser works.
This doesn't work:
URL url = new URL(signedUrl);
However, pasting the output of System.out.println(signedUrl)
into the URL object does work.
System.out.println(signedUrl);
URL url = new URL("https://storage.googleapis.com/rydr/insurance_documents/test123.png?X...");
The exception:
java.lang.RuntimeException: java.net.MalformedURLException: no protocol: "https://storage.googleapis.com/rydr/insurance_documents/test123.png?X..."
Several years ago, a StackOverflow user had a similar problem here, but the suggestions of using .trim()
failed to resolve the issue for me.
I also tried using DownloadManager, but it throws a similar exception:
java.lang.IllegalArgumentException: Can only download HTTP/HTTPS URIs: "https://storage.googleapis.com/rydr/insurance_documents/test123.png?X..."
After viewing the PHP script which generates the signed URL in the browser, I found that it was outputting the URL inside double quotes.
Manually removing the double quotes in Java fixed the problem:
URL url = new URL(signedUrl.replace("\"", ""));