【问题标题】:Android download from Azure blob storage results in invalid file从 Azure Blob 存储下载 Android 会导致文件无效
【发布时间】:2012-09-11 21:45:03
【问题描述】:

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

当我从我的应用程序中下载生成的文件时,它已损坏。当我使用默认浏览器或 Chrome 下载它时,也会出现同样的错误。同样来自 Easy Downloader 应用程序,也会出现同样的错误。仅从我的 PC 下载或从 Android 设备(或模拟器)使用 Firefox Beta 即可正确检索文件。

我使用以下代码(sn-p):

    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文件无效。

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

有没有人在从 Azure 下载时遇到过类似的问题?有人可以提供解决方案吗?

干杯, 哈拉尔...

【问题讨论】:

    标签: android azure blob


    【解决方案1】:

    您是否尝试过在您的安卓应用中使用azure-sdk-for-java

    我们的场景略有不同,因为我们使用 sdk 将图像从 blob 存储拉取和推送到自定义 Android 应用程序。但基本原理应该是一样的。

    【讨论】:

    • 可能还想检查文件的元数据类型。我发现这有时会导致问题。
    • 我想到了Azure SDK,但是文件一直不在Azure存储上;很难区分这些情况。
    • 文件的Content-Type标签在所有情况下都是video/mp4。
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 2019-09-05
    • 2020-08-08
    • 2015-06-20
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多