java - How can I properly download a file from Google Cloud Storage to an Android app? -
first of all, can download gcs using downloadmanager, gcs java api, , android asynchttpclient. resulting file or file stream has headers in it, prevents file being opened properly.
example file saved 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 ...
what libraries available or how use of libraries i've tried save file not have embedded header information? preferably, use header information.
thanks!
here code got working me. able use /r/n/r/n delimiter. note, modified storagefactory.java use androidhttp.newcompatibletransport() , use p12 keyfile. version here if figure out why git not adding changes since last week.
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: (int = 0; < b.length - delimiter.length + 1; i++) { (int j = 0; j < delimiter.length; j++) { if (b[i + j] != delimiter[j]) { continue outer; } } bytearrays.add(arrays.copyofrange(b, begin, i)); begin = + 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();
Comments
Post a Comment