Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – java.io.FileNotFoundException: with working URL?

Posted on November 11, 2022 By

Solution 1 :

You could try using the HTTPS protocol instead of the HTTP protocol, as described here.

Problem :

I switched my hosting provider from Hostinger to Digital Ocean Spaces, the files are the same, only the url is different.

Playing the hosted mp3 file with media player still works with the new URL but downloading it doesn’t.

I get the following crash:

    java.io.FileNotFoundException: https://myapp-content.ams3.digitaloceanspaces.com/MyApp/sounds/b9a14d77-d122-4bd9-9f2d-5dd7ad9b90ee.mp3
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
    at com.myapp.d.EventHandlerClass$4.run(EventHandlerClass.java:252)
at java.lang.Thread.run(Thread.java:920)

The link is working (not the one I posted here) even if I access it in inkognito, i can also download the sound in chrome.
But somehow the downloading process in my app doesnt work with the mp3 files hosted on Digital Ocean.
Any Ideas?

This line is causing the problem:

InputStream inputStream = urlConnection.getInputStream();

With the old hosting provider I’m getting status code 200, with the new one 400

Here is the whole download Function:

try {
                URL url = new URL(filePath);
                HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();

                File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                File directory = new File(storage.getAbsolutePath());
                directory.mkdirs();


                String fileNameWithoutSpecialCharacters = fileName.replaceAll("\W", "");
                file = new File(directory, fileNameWithoutSpecialCharacters + ".mp3");

                FileOutputStream fileOutput = new FileOutputStream(file);

                Log.i("download:", "Code: " + urlConnection.getResponseCode());
                InputStream inputStream = urlConnection.getInputStream();

                totalSize = urlConnection.getContentLength();

                int downloadedSize = 0;

                byte[] buffer = new byte[1024];

                int bufferLength = 0; //used to store a temporary size of the buffer

                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fileOutput.write(buffer, 0, bufferLength);
                    downloadedSize += bufferLength;
                    Log.i("download:", downloadedSize + "/" + totalSize);
                }

                fileOutput.close();
                ClipboardManager clipboard = (ClipboardManager) MainActivity.mainActivityWeakReference.get().getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("test", "test");
                clipboard.setPrimaryClip(clip);

            } catch (IOException e) {
                e.printStackTrace();
                ClipboardManager clipboard = (ClipboardManager) MainActivity.mainActivityWeakReference.get().getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("test", e.toString());
                clipboard.setPrimaryClip(clip);
            }

Comments

Comment posted by luckrd

The HTTP code 400 means that, from the server’s point of view, an incorrect request was sent. Please include some more code so that we can understand how the request is generated.

READ  [FIXED] android - Save image from internal storage to gallery through ACTION_VIEW intent
Powered by Inline Related Posts

Comment posted by HavanaSun

@Sapphirex I added the download function to my question. This function works perfectly fine with my old URL

Comment posted by HavanaSun

Unfortunately this is not working (I updated my question with the new code) The files hosted on Hostinger work no matter if its http or https, the files on DigitalOcean don’t work at all. Here is a working link with a audio file which is not downloadable

Comment posted by memedex-content.ams3.digitaloceanspaces.com/testsound/…

memedex-content.ams3.digitaloceanspaces.com/testsound/…

Android Tags:android, inputstream, java, url

Post navigation

Previous Post: [FIXED] android developer api – there is a ‘Late Initialization Error’ during flutter development
Next Post: [FIXED] android – How to set SSL pinning using retrofit for Firebase Realtime Database Url?

Related Posts

[FIXED] javascript – Three.JS Rotate Camera around object by moving the device Android
[FIXED] nullpointerexception – probleme with android.text.Editable android.widget.EditText.getText() on a null object reference Android
[FIXED] android – Json Placeholder API – fetching photos for specific user Android
[FIXED] android – Custom buttons in exoplayer Android
[FIXED] android – NullPointerException whlie changing startactivity Android
[FIXED] How to enable viewBinging in android project using Kotlin gradle dsl? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?
  • Is blue raspberry an original Jolly Rancher flavor?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme