从Azure blob存储中下载Android会导致文件无效

时间:2012-09-19 14:45:58

标签: android azure blob

在我的Android应用中,我尝试使用以下网址从Windows Azure blob存储中下载:http://iclyps.blob.core.windows.net/broadcasts/23_6.mp4

当我从我的应用程序中下载时,生成的文件已损坏。使用默认浏览器或Chrome下载时出现相同的错误。同样来自Easy Downloader应用程序,也会发生同样的错误。只需从我的电脑下载或从Android设备(或仿真器)使用Firefox Beta,即可正确检索该文件。

我使用以下代码(代码段):

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

        //set up some things on the connection
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        //and connect!
        urlConnection.connect();

        bis = new BufferedInputStream(urlConnection.getInputStream(), BUFSIZE);
        bos = new BufferedOutputStream(
                context.openFileOutput(TMPFILE, Context.MODE_PRIVATE), BUFSIZE);
        /*
         * Read bytes to the buffer in chunks of BUFSIZE bytes until there is nothing more to read.
         * Each chunk is written to the output file.
         */
        byte[] buf = new byte[BUFSIZE];
        int nBytes = 0;
        int tBytes = 0;
        while ((nBytes = bis.read(buf, 0, BUFSIZE)) > 0) {
            bos.write(buf, 0, nBytes);
            tBytes += nBytes;
        }
        if (tBytes == 0) throw new Exception("no bytes received");
        bos.flush();
        MobyLog.d(TAG, "download succeeded: #bytes = " + Integer.toString(tBytes));
        return true;
    } catch (Exception e) {
        MobyLog.e(TAG, "download failed: " + e);
        context.deleteFile(TMPFILE);    // remove possibly present partial file.
        return false;
    } finally {
        if (bis != null) try { bis.close(); } catch (IOException e) {MobyLog.e(TAG, "bis close exception: " + e); };
        if (bos != null) try { bos.close(); } catch (IOException e) {MobyLog.e(TAG, "bos close exception: " + e); };
    }

分析文件显示原始文件的第一部分(约700K)在损坏的文件中重复多次,导致mp4文件无效。

将文件放在另一个Web服务器(Apache / IIS)上,并从该位置下载文件会导致正确下载。

是否有人遇到类似的问题从Azure执行下载?有人可以提供解决方案吗?

干杯, 哈拉尔...

1 个答案:

答案 0 :(得分:0)

您是否尝试在Android应用中使用azure-sdk-for-java

我们的情况略有不同,因为我们使用sdk将图像从blob存储拉出并推送到自定义Android应用程序。但基本面应该是一样的。