In an android app how to download a blob from google appengine [duplicate]

Mobile Technologies Mobile Computing 2 years ago

8.28K 1 0 0 0

_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ Download a file with Android, and showing the progress in a ProgressDialog_x000D_ _x000D_ 12 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ I have an appengine application serving zip files from the blob-store. When I use this URL+query : http://spiceappcloud.appspot.com/bundle-service?spice_category=Education&spice_sub_category0=Topography&spice_sub_category1=Outdoor%20Skills&spice_name=Topo.zip from Chrome or Firefox the file gets downloaded! How do I download the same file in a service within an android app? I would appreciate code.

Posted on 16 Aug 2022, this text provides information on Mobile Computing related to Mobile Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ OK I got it through trial and error using using an HttpClient & an HttpGet. private boolean downloadFile(String query) { Log.d(TAG, "trying to download file"); boolean downloadSuccessful = false; if(!fileName.endsWith(".zip")) { fileName += ".zip"; Log.d(TAG, "added zip to file name"); } is = null; bis = null; fos = null; bos = null; outFile = null; int counter = 0; while(!downloadSuccessful && counter < 5) { Log.d(TAG + ".downloadFile()", "attempt number: " + counter); try { //connect(query); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(query); HttpResponse response = null; try { response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); Log.d(TAG, "statusCode: " + statusCode); if (statusCode == 200) { HttpEntity entity = response.getEntity(); is = entity.getContent(); bis = new BufferedInputStream(is); } else { Log.e(TAG, "Failed to download file"); } } catch (ClientProtocolException e) { Log.e(TAG, e.getMessage()); } catch (IOException e) { Log.e(TAG, e.getMessage()); } Header[] hs = response.getAllHeaders(); if(hs == null) { Log.e(TAG, "Headers are null "); return false; } for(Header h: hs) { Log.d(TAG, "Header values: " + h.getValue()); } fileSize = 200000; if(counter == 0) { Log.e(TAG, "File size is: " + fileSize); } else { Log.e(TAG, "This is what's left to download: " + fileSize); } sendNotifiction("Connecting", "Connecting...", "Spice bundle: " + fileName); updateUser(MESSAGE_CONNECTING_STARTED, fileSize / 1024); outFile = new File(ZIP_LOCATION + "/" + fileName); fos = new FileOutputStream(outFile); bos = new BufferedOutputStream(fos, DOWNLOAD_BUFFER_SIZE); data = new byte[DOWNLOAD_BUFFER_SIZE]; bytesRead = 0; totalRead = 0; downloadSuccessful = download(); Log.d(TAG, "Total bytes Read: " + totalRead + " of: " + fileSize); if(totalRead == fileSize) { downloadSuccessful = true; Log.e(TAG, "Download successful use this message wairily"); } else { Log.e(TAG, "Download fubar"); } } catch(FileNotFoundException e) { Log.e(TAG, "FileNotFoundException: " + e.getMessage()); notificationFlash = "Error downloading"; notificationTitle = "Error"; notificationText = "Spice bundle: " + fileName; sendNotifiction(notificationFlash, notificationTitle, notificationText); updateUser(MESSAGE_DOWNLOAD_ERROR, 0); } catch(NullPointerException e) { Log.e(TAG, "NullPointerException"); } catch(IllegalStateException e) { Log.e(TAG, e.getMessage()); } catch(Exception e) { Log.e(TAG, "Exception: " + e.getMessage()); notificationFlash = "Error downloading"; notificationTitle = "Error"; notificationText = "Spice bundle: " + fileName; sendNotifiction(notificationFlash, notificationTitle, notificationText); updateUser(MESSAGE_DOWNLOAD_ERROR, 0); } counter++; } try { if(bos != null) { bos.close(); } if(fos != null) { fos.close(); } if(bis != null) { bis.close(); } if(is != null) { is.close(); } } catch (IOException e) { Log.e(TAG + " given up downloading", "IOException thrown"); notificationFlash = "Error downloading"; notificationTitle = "Error"; notificationText = "Spice bundle: " + fileName; sendNotifiction(notificationFlash, notificationTitle, notificationText); updateUser(MESSAGE_DOWNLOAD_ERROR, 0); } return downloadSuccessful; } private boolean download() { boolean downloadComplete = false; try { while(!this.isCancelled()) { bytesRead = bis.read(data, 0, data.length); if( bytesRead < 0) { downloadComplete = true; break; } bos.write(data, 0, bytesRead); totalRead += bytesRead; int totalReadInKB = totalRead / 1024; if(totalReadInKB % 100 == 0) { Log.d(TAG, "Total Read In KB: " + totalReadInKB + " of: " + fileSize/1024); updateUser(MESSAGE_UPDATE_PROGRESS_BAR, totalReadInKB); } } } catch(IOException e) { Log.e(TAG + ".download()", e.getMessage()); Log.e(TAG + ".download()", "IOException thrown download incomplete"); } finally { try { bos.flush(); } catch (IOException e) { Log.e(TAG + ".download()", e.getMessage()); Log.e(TAG + ".download()", "IOException thrown couldn't flush"); } } return downloadComplete; }
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum


Q

Would Firebase be a good choice as a server for the Flutter mobile game I'm creating?

_x000D_ _x000D_ I am building a turn-based Flutter game. Would Firebase be a good choice as a server...
Q

Implement Bottom Navigation activity along with a VerticalViewPager with nested Cardview in Android...

_x000D_ _x000D_ I want to develop an Android App which will have a Bottom Navigation Bar and above i...
Q

(JS) For , determine if the device offers or not to capture a photo from the camera

_x000D_ _x000D_ When uploading a file, I need to give the user the possibility: upload the file fr...

Important Mobile Technologies Links