Uploading Multiple Images To Server
Solution 1:
Have you considered using FTP instead of your present methodology? There is a library from apache called Apache's commons-net-ftp library to get the job done easily.
Here is a question I asked on stackoverflow long time ago. I was implementing pretty much the same code before but I found FTP method is way easier for uploading files to server.
@TemporaryNickName How to send other data too along with that image. Moreover my image data is in bitmap, not an uri. How to implement it according to my present things?
*There are many tutorials that shows you how to convert bitmap to image file (This is pretty much method of saving file temporary and delete immediately after FTP is done), moreover, when you take a photo using default built in camera app, you image saves automatically. Well sending data should be done separately, in this case, I would write a PHP script with $_POST to receive data than save it into database or write it into XML*
To save uploaded file, use
move_uploaded_file($src, $path);
Solution 2:
To send multiple types of data use MultipartEntity
(from link provided in your question) instead of URLEncodedEntity
. The idea is that MultipartEntity
just contains bodies of different types (such as StringBody
, FileBody
, etc.). So, if you need to send images in Base64, add them as StringBody
to the MultipartEntity
(which should be setted as entity of your request, using setEntity
).
Although, I highly recommend you to save your bitmaps on disk (sd card) and use FileBody
instead. It will save you a lot of memory (with Base64 you have to load all images at once) and... what if user will close your app while uploading? You'll lost your bitmaps forever.
P.S. Don't forget to use Service
for uploading tasks.
Solution 3:
Here is my code snippet, Hope this helps :
privateclassAsyncTask1extendsAsyncTask<Void, Void, String>{
@Overrideprotected String doInBackground(Void... params) {
booleanresponse=false;
try {
ByteArrayOutputStreambos=newByteArrayOutputStream();
FileBodybin=newFileBody(newFile("temp"));
FiletempImg=newFile("sdcard/signature.jpg");
if(tempImg.exists())
{
checkimgfile=checkimgfile+"LPA"+tempImg;
bin = newFileBody(tempImg, "image/jpg");
reqEntity.addPart("txt_sign_lpa", bin);
reqEntity.addPart("count_lpa",newStringBody("1"));
}
else
{
reqEntity.addPart("count_lpa",newStringBody("0"));
}
FileBodybin1=newFileBody(newFile("temp"));
FiletempImg1=newFile("sdcard/signature2.jpg");
if(tempImg1.exists())
{
checkimgfile=checkimgfile+"subject"+tempImg1;
bin1 = newFileBody(tempImg1, "image/jpg");
reqEntity.addPart("txt_sign", bin1);
reqEntity.addPart("count_subject",newStringBody("1"));
}
reqEntity.addPart("count",newStringBody("0"));
reqEntity.addPart("name",newStringBody("Shaili"));
reqEntity.addPart("age",newStringBody("47"));
try
{
ConnectivityManagercm=
(ConnectivityManager)getBaseContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfoactiveNetwork= cm.getActiveNetworkInfo();
if(activeNetwork!=null && activeNetwork.isAvailable() && activeNetwork.isConnected())
{
Stringxml="";
HttpParamshttpParameters=newBasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 100000);
HttpConnectionParams.setSoTimeout(httpParameters, 100000);
finalHttpClienthttpclient=newDefaultHttpClient(httpParameters);
finalHttpPosthttppost=newHttpPost("https://www.xyz.com/abc.php");//url where you want to post your data.
httppost.setParams(httpParameters);
httppost.setEntity(reqEntity);
httppost.addHeader("Accept", "text/html");
httppost.addHeader("Host", "www.xyz.com");
httppost.addHeader("User-Agent",
"Android ");
HttpResponseresponse1=null;
StringerrMessage="Error";
try {
response1 = httpclient.execute(httppost);
finalHttpEntityresEntity= response1.getEntity();
InputStreamcontent= resEntity.getContent();
BufferedReaderb=newBufferedReader(newInputStreamReader(
content));
xml = XmlParser.getTrimmedResponse(b);
if (response1 != null){
if(Integer.toString(response1.getStatusLine().getStatusCode()).equals("200")){
return"success";
}
}
} catch (Exception e) {
e.printStackTrace();
errorstring=errorstring+e.getLocalizedMessage();
errMessage = "Network error";
return errMessage;
}
}
elseif(activeNetwork==null)
{
return"Available";
}
}
catch(Exception e)
{
Toast.makeText(getBaseContext(), "Network Connection not available", 1).show();
progressDialog.dismiss();
}
} catch (Exception e) {
errorstring=errorstring+e.getLocalizedMessage();
return"Network error";
}
return"abc";
}
protectedvoidonPostExecute(String result) {
//do your stuff
}
}
Post a Comment for "Uploading Multiple Images To Server"