Try this code. This is useful for one or two or three image uploading, and Use httpmime-4.2.1.jar
Activity
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
public class MultiImageActivity extends Activity {
private static final int CAMERA_PICTURE = 1;
private static final int GALLERY_PICTURE = 2;
String filePath1 = null;
String filePath2 = null;
String filePath3 = null;
Bitmap chosenImage;
File destination = null;
ImageView ivImg1, ivImg2, ivImg3;
String img1Selected1 = null, img2Selected1 = null, img3Selected1 = null;
String img1Selected2 = null, img2Selected2 = null, img3Selected2 = null;
LinearLayout llImgHolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_image);
Button btnImg1 = (Button) findViewById(R.id.btn_multi_img_1);
Button btnImg2 = (Button) findViewById(R.id.btn_multi_img_2);
Button btnImg3 = (Button) findViewById(R.id.btn_multi_img_3);
Button btnUpload = (Button) findViewById(R.id.btn_multi_upload);
llImgHolder = (LinearLayout) findViewById(R.id.ll_multi_img_holder);
ivImg1 = (ImageView) findViewById(R.id.iv_multi_img1);
ivImg2 = (ImageView) findViewById(R.id.iv_multi_img2);
ivImg3 = (ImageView) findViewById(R.id.iv_multi_img3);
btnImg1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
img1Selected1 = "fulfilled";
choosePictureAction();
}
});
btnImg2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (img1Selected2 != null) {
img2Selected1 = "fulfilled";
choosePictureAction();
}else {
Log.e("Please select first image", "Please select first image");
}
}
});
btnImg3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (img2Selected2 != null) {
img3Selected1 = "fulfilled";
choosePictureAction();
}else {
Log.e("Please select second image", "Please select second image");
}
}
});
btnUpload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url="______________your_api________";
new uploadAsynTask().execute(url);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_PICTURE && resultCode == RESULT_OK) {
chosenImage = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
chosenImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
destination = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
//filePath1 = destination.toString();
if(img1Selected1 != null){
filePath1 = destination.toString();
}else if(img2Selected1 != null){
filePath2 = destination.toString();
}else if(img3Selected1 != null){
filePath3 = destination.toString();
}
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(chosenImage != null){
if(img1Selected1 != null){
llImgHolder.setVisibility(View.VISIBLE);
ivImg1.setVisibility(View.VISIBLE);
img1Selected1 = null;
img1Selected2 = "fulfilled";
ivImg1.setImageBitmap(chosenImage);
}else if(img2Selected1 != null){
ivImg2.setVisibility(View.VISIBLE);
img2Selected1 = null;
img2Selected2 = "fulfilled";
ivImg2.setImageBitmap(chosenImage);
}else if(img3Selected1 != null){
ivImg3.setVisibility(View.VISIBLE);
img3Selected1 = null;
img3Selected2 = "fulfilled";
ivImg3.setImageBitmap(chosenImage);
}
}
} else if (requestCode == CAMERA_PICTURE
&& resultCode == RESULT_CANCELED) {
} else if (requestCode == GALLERY_PICTURE
&& resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String[] projection = { MediaColumns.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
String selectedImagePath = cursor.getString(column_index);
destination = new File(selectedImagePath);
if(img1Selected1 != null){
filePath1 = selectedImagePath;
}else if(img2Selected1 != null){
filePath2 = selectedImagePath;
}else if(img3Selected1 != null){
filePath3 = selectedImagePath;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(selectedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
chosenImage = BitmapFactory.decodeFile(selectedImagePath, options);
if(chosenImage!=null){
if(img1Selected1 != null){
llImgHolder.setVisibility(View.VISIBLE);
ivImg1.setVisibility(View.VISIBLE);
img1Selected1 = null;
img1Selected2 = "fulfilled";
ivImg1.setImageBitmap(chosenImage);
}else if(img2Selected1 != null){
ivImg2.setVisibility(View.VISIBLE);
img2Selected1 = null;
img2Selected2 = "fulfilled";
ivImg2.setImageBitmap(chosenImage);
}else if(img3Selected1 != null){
ivImg3.setVisibility(View.VISIBLE);
img3Selected1 = null;
img3Selected2 = "fulfilled";
ivImg3.setImageBitmap(chosenImage);
}
}
} else if (requestCode == GALLERY_PICTURE
&& resultCode == RESULT_CANCELED) {
}
}
private void choosePictureAction(){
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(MultiImageActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(items[which].equals("Camera")){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PICTURE);
}else if(items[which].equals("Gallery")){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_PICTURE);
}else if(items[which].equals("Cancel")){
dialog.dismiss();
}
}
});
builder.show();
}
private class uploadAsynTask extends AsyncTask<String, Void, String>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(MultiImageActivity.this, null, null);
ProgressBar spinner = new android.widget.ProgressBar(MultiImageActivity.this, null,android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(Color.parseColor("#009689"), android.graphics.PorterDuff.Mode.SRC_IN);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(spinner);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
Log.e("FilePathBin1", filePath1);
File file1 = new File(filePath1);
File file2 = null;
File file3 = null;
if(img2Selected2 != null){
Log.e("FilePathBin2", filePath2);
file2 = new File(filePath2);
}
if(img3Selected2 != null){
Log.e("FilePathBin3", filePath3);
file3 = new File(filePath3);
}
MultipartEntity reqEntity;
HttpEntity resEntity;
try {
HttpClient client = new DefaultHttpClient();
String postURL = params[0];
HttpPost post = new HttpPost(postURL);
FileBody bin1 = new FileBody(file1);
FileBody bin2 = null;
FileBody bin3 = null;
if(img2Selected2 != null){
bin2 = new FileBody(file2);
}
if(img3Selected2 != null){
bin3 = new FileBody(file3);
}
reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("parking_title", new StringBody("Test13:26"));
reqEntity.addPart("latitude", new StringBody("10.12313213"));
reqEntity.addPart("longtitude", new StringBody("12.123213213"));
reqEntity.addPart("note", new StringBody("12"));
reqEntity.addPart("filename[0]", bin1);
if(img2Selected2 != null){
img2Selected2 = null;
reqEntity.addPart("filename[1]", bin2);
}
if(img3Selected2 != null){
img3Selected2 = null;
reqEntity.addPart("filename[2]", bin3);
}
reqEntity.addPart("spot_types[0]", new StringBody("1"));
reqEntity.addPart("spot_types[1]", new StringBody("2"));
reqEntity.addPart("spot_types[2]", new StringBody("3"));
reqEntity.addPart("spot_properties[0]", new StringBody("1"));
reqEntity.addPart("spot_properties[1]", new StringBody("2"));
reqEntity.addPart("spot_properties[2]", new StringBody("3"));
post.setEntity(reqEntity);
HttpResponse response = client.execute(post);
resEntity = response.getEntity();
String entityContentAsString = EntityUtils.toString(resEntity);
Log.d("stream:", entityContentAsString);
return entityContentAsString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != null){
dialog.dismiss();
Log.e("addPhotoResult", result);
filePath1 = null;
filePath2 = null;
filePath3 = null;
try {
JSONObject jsonObject = new JSONObject(result);
String error = jsonObject.getString("Error");
String message = jsonObject.getString("message");
Log.e("error", error);
Log.e("message", message);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
Xml Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:context="com.app.multiimage.MultiImageActivity" >
<Button
android:id="@+id/btn_multi_img_1"
android:layout_width="match_parent"
android:layout_height="35dp"
android:background="#000000"
android:text="@string/multi_img1"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<Button
android:id="@+id/btn_multi_img_2"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_below="@+id/btn_multi_img_1"
android:layout_marginTop="10dp"
android:background="#000000"
android:text="@string/multi_img2"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<Button
android:id="@+id/btn_multi_img_3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_below="@+id/btn_multi_img_2"
android:layout_marginTop="10dp"
android:background="#000000"
android:text="@string/multi_img3"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<LinearLayout
android:id="@+id/ll_multi_img_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_multi_img_3"
android:layout_marginTop="10dp"
android:visibility="gone"
android:orientation="horizontal" >
<ImageView
android:id="@+id/iv_multi_img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"
android:visibility="gone"
android:contentDescription="@string/empty" />
<ImageView
android:id="@+id/iv_multi_img2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@+id/iv_multi_img1"
android:layout_weight="1"
android:visibility="gone"
android:contentDescription="@string/empty" />
<ImageView
android:id="@+id/iv_multi_img3"
android:layout_width="100dp"
android:layout_height="100dp"
android:visibility="gone"
android:layout_toRightOf="@+id/iv_multi_img2"
android:layout_weight="1"
android:contentDescription="@string/empty" />
</LinearLayout>
<Button
android:id="@+id/btn_multi_upload"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_below="@+id/ll_multi_img_holder"
android:layout_marginTop="10dp"
android:background="#000000"
android:text="@string/multi_upload"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</RelativeLayout>
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />