I'd like to send an image from the Android client to the Jango server using Http Post. The image will be selected in the instrument's gallery. We are currently using the NameValuePair list to send essential data to the server and to get a response in JSON form from the warehouse. Is it okay to use the same method for sending and receiving images (including image url in JSON-type responses)?
And, how to access the image remotely without downloading the image from the server, or how to use the image locally after downloading the image and storing it in a Bitmap array. Which is better? The number of images is 10 or less and the size is about 50*50dip.
It would be nice to have a tutorial on this issue.
Add: Adjusting the selected image in the gallery to the appropriate size will send it to the server.
android django http
Let's start by assuming that you know the path and file name of the image you want to upload. Add this string to the NameValuePair and use image as the key-name.
You can transfer images using Http Components libraries. After downloading the latest version of HttpClient with the Depends package, copy the apache-mime4j-0.6.jar and httpmime-4.0.1.jar files to your project and add them to the Java build path.
Add the import statement below to the class.
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
You can now create MultipartEntities to attach images to POST requests. Please check the code below to see how to use it.
public void post(String url, List<NameValuePair> nameValuePairs) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
for(int index=0; index < nameValuePairs.size(); index++) {
if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
// Use FileBody for data transfer when key value is image
entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
} } else {
// Just string data
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost, localContext);
} } catch (IOException e) {
e.printStackTrace();
}
}
I hope my answer is helpful.
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
578 Understanding How to Configure Google API Key
619 Uncaught (inpromise) Error on Electron: An object could not be cloned
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.