How to download a bitmap from an Android device?

Thanks in advance. I would like to download some raster image from my Android app. but I can’t get it. Could you recommend some solutions for this. or compile my source code?

ByteArrayOutputStream bao = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://example.com/imagestore/post"); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE ); byte [] ba = bao.toByteArray(); try { entity.addPart("img", new StringBody(new String(bao.toByteArray()))); httppost.setEntity(entity); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Execute HTTP Post Request HttpResponse response = null; try { response = httpclient.execute(httppost); } catch (ClientProtocolException e) { } 
+6
java android multipartform-data
source share
2 answers
+3
source share

I found that this solution is really well created and 100% works even with amazon ec2, look at this link:

Uploading files to an HTTP server using POST on Android (link removed).

Compare with the previous answer, this solution does not require importing a huge httpmime library from Apache.

Copied text from the original article:

This tutorial shows an easy way to upload data (images, MP3s, text files, etc.) to an HTTP / PHP server using the Android SDK.

It includes all the code needed to complete the download on the Android side, as well as simple server-side code in PHP to process the file’s download and save it. In addition, it also gives you information on how to handle basic authorization when downloading a file.

When testing on an emulator, be sure to add the test file to the Androids file system via DDMS or on the command line.

What we are going to do is set the appropriate type of request content and include an array of bytes as the message body. The byte array will contain the contents of the file that we want to send to the server.

Below you will find a useful piece of code that performs a load operation. The code also includes server response processing.

 HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; String pathToOurFile = "/data/file_to_send.mp3"; String urlServer = "http://192.168.1.1/handle_upload.php"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1*1024*1024; try { FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) ); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs. connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Set HTTP method to POST. connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); outputStream = new DataOutputStream( connection.getOutputStream() ); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) serverResponseCode = connection.getResponseCode(); serverResponseMessage = connection.getResponseMessage(); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { //Exception handling } 

If you need to authenticate your user with a username and password when downloading a file, the code snippet below shows how to add it. All you have to do is set authorization headers when creating the connection.

 String usernamePassword = yourUsername + ":" + yourPassword; String encodedUsernamePassword = Base64.encodeToString(usernamePassword.getBytes(), Base64.DEFAULT); connection.setRequestProperty ("Authorization", "Basic " + encodedUsernamePassword); 

Let's say that the PHP script is responsible for receiving data on the server side. An example of such a PHP script might look like this:

 <?php $target_path = "./"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "There was an error uploading the file, please try again!"; } ?>; 

The code has been tested on Android 2.1 and 4.3. Remember to add permissions to your server side script. Otherwise, the download will not work.

 chmod 777 uploadsfolder 

Where uploadsfolder is the folder where files are uploaded. If you plan to upload files larger than the default 2MB file size. You will need to change the upload_max_filesize value in the php.ini file.

+1
source share

All Articles