1

Got an weird issue. A file with Url: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3 (not available any more) downloads ok on PC and its mp3 file. But when I try to DL it on Android FOA Im getting content-type "application/xml" instead of "audio/mpeg" and when downloading starts I'm getting:

05-30 12:13:44.478: E/PlayerService(28023): java.io.FileNotFoundException: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)

The code used to DL:

    /**
     * Download the url stream to a temporary location
     */
    public void downloadAudioIncrement(String mediaUrl) throws IOException {
        Log.i(TAG, "downloadAudioIncrement(): mediaUrl: "+mediaUrl+"\ncacheDir: "+cacheDir);

        URL url = null;

        try {
            url = new URL(mediaUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IOException("Unable to create InputStream for mediaUrl:" + mediaUrl);
        }

        // this file will represent whole downloaded song
        mp3FileDownloaded = new File(cacheDir, mp3FileName);
        if (!mp3FileDownloaded.exists())
            //FileUtils.makeDirsForFile(mp3FileDownloaded);
            try{
                mp3FileDownloaded.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
        }

        if (!mp3FileDownloaded.canWrite())
            throw new IOException("Can't open temporary file for writing");

        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

        urlConnection.setReadTimeout(1000 * 20);
        urlConnection.setConnectTimeout(1000 * 5);

        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        int mp3BytesSize = urlConnection.getContentLength();
        // final String
        // contentLengthStr=urlConnection.getHeaderField("content-length");

        String ctype = urlConnection.getContentType();
        if (ctype == null) {
            ctype = "";
        } else {
            ctype = ctype.toLowerCase(Locale.US);
        }
        // See if we can handle this type
        Log.i(TAG, "Content Type: " + ctype);

        if ( ctype.contains("audio/mpeg") || TextUtils.isEmpty(ctype) ) {

            String temp = urlConnection.getHeaderField(BITRATE_HEADER);
            Log.i(TAG, "Bitrate: " + temp);
            // if (temp != null){
            // bitrate = new Integer(temp).intValue();
            // }
        } else {
            Log.e(TAG, UNSUPPORTED_AUDIO_TYPE+": " + ctype);
//          throw new IOException(UNSUPPORTED_AUDIO_TYPE+": " + ctype);
            // Log.e(TAG, "Or we could not connect to audio");
            // stop();
            // return;
        }
        final InputStream stream = new BufferedInputStream(urlConnection.getInputStream(),8192);
...

Right at the last shown line of code (instantiating the InputStream stream) the mentioned IOExeption raised. There are other mp3 files exists at same location and they are downloading with no any issue but only mentioned above url fails.
What could be wrong here?
UPDATE
Its appears that this issue happens on HTC Rezound with AOS 4.0.4. On other device, with AOS 2.3.5 everything works ok.

Stan
  • 6,511
  • 8
  • 55
  • 87

1 Answers1

1

seems like the line

urlConnection.setDoOutput(true);

was the source of issue since I don't upload any data. Everything works fine since I'd comment it. Also these FileNotFoundException while getting the InputStream object from HttpURLConnection and Android HttpUrlConnection getInputStream throws NullPointerException threads might be helpfull.

Community
  • 1
  • 1
Stan
  • 6,511
  • 8
  • 55
  • 87