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

时间:2016-04-19 03:19:23

标签: java android google-cloud-storage

首先,我可以使用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
...

哪些库可用或如何使用我尝试正确保存文件的任何库,因此它没有嵌入的标题信息?最好,我想使用标题信息。

谢谢!

1 个答案:

答案 0 :(得分:0)

以下是让我这样做的代码。我能够使用/ r / n / r / n作为分隔符。注意,我修改了StorageFactory.java以使用AndroidHttp.newCompatibleTransport()并使用p12密钥文件。如果我能弄清楚为什么git自上周以来没有添加我的更改,那么我的版本将为here

            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();