繁体   English   中英

如何将文件从Google云端存储正确下载到Android应用?

[英]How can I properly download a file from Google Cloud Storage to an Android app?

首先,我可以使用DownloadManager,GCS Java API和Android AsyncHttpClient从GCS下载。 但是生成的文件或文件流中包含标头,从而阻止了文件的正确打开。

由DownloadManager / GCS api / Android AsynHttpClient保存的示例文件:

--5tc52jlLclf7f49cCw5hDvB1BwmZZB
Content-Disposition: form-data; name="example.pdf"; filename="example.pdf"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

%PDF-1.4
%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com
% 'BasicFonts': class PDFDictionary
...

有哪些可用的库?如何使用我尝试正确保存文件的任何库,使其没有嵌入的标头信息? 优选地,我想使用标题信息。

谢谢!

这是使该代码对我有用的代码。 我能够使用/ r / n / r / n作为分隔符。 注意,我修改了StorageFactory.java以使用AndroidHttp.newCompatibleTransport()并使用p12密钥文件。 如果我能弄清楚为什么git自上周以来未添加更改,我的版本就会在这里

            new AsyncTask<Void, Void, GoogleCredential>() {
                @Override
                protected GoogleCredential doInBackground(Void... view) {
                    try {
                        Storage storage = StorageFactory.getService(credential);
                        Storage.Objects.Get getObject = storage.objects().get(BUCKET_NAME, fileName);
                        // Downloading data.
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true);
                        getObject.executeMediaAndDownloadTo(out);

                        OutputStream outputStream = new FileOutputStream (
                                getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                                        + "/" + fileName);


                        byte[] b = out.toByteArray();

                        final byte[] delimiter = BaseEncoding.base16().lowerCase().decode("0d0a0d0a".toLowerCase());
                        List<byte[]> byteArrays = new LinkedList<>();
                        int begin = 0;
                        outer:
                        for (int i = 0; i < b.length - delimiter.length + 1; i++) {
                            for (int j = 0; j < delimiter.length; j++) {
                                if (b[i + j] != delimiter[j]) {
                                    continue outer;
                                }
                            }
                            byteArrays.add(Arrays.copyOfRange(b, begin, i));
                            begin = i + delimiter.length;
                            break;
                        }
                        byteArrays.add(Arrays.copyOfRange(b, begin, b.length));

                        outputStream.write(byteArrays.get(1));
                        outputStream.close();
                        Log.i(LOG_TAG, "Created: " +
                                getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()
                                + "/" + fileName);

                    } catch (GeneralSecurityException e) {
                        Log.e(LOG_TAG, "GCS Api fail: " + e);
                    } catch (IOException e) {
                        Log.e(LOG_TAG, "GCS Api fail: " + e);
                    } catch (NullPointerException e) {
                        Log.e(LOG_TAG, "GCS Api fail: " + e);
                    }
                    return credential;
                }
                @Override
                protected void onPostExecute(final GoogleCredential credential) {
                    Log.i(LOG_TAG, "GCS completed.");
                    // Show file

                }
            }.execute();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM