Monday, 7 December 2015

get images from camera/galary and upload to web service

Step 1> write code in MainActivity.java

public class MainActivity extends Activity {


private static final int CAMERA_REQUEST = 1888;
//from galary
    private static int RESULT_LOAD_IMAGE = 1;
        ImageView iv1,iv;
Bitmap  bmpprof;
Bitmap  bmp;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
                //write what ever you required
         }

///uploading profile image

public void uploadprofilepic(View v)
{
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.alert);
dialog.show();
dialog.setTitle("ProfilePic");
ImageView btn1=(ImageView)dialog.findViewById(R.id.button1);
ImageView btn2=(ImageView)dialog.findViewById(R.id.button2);
     ImageView iv=(ImageView)dialog.findViewById(R.id.imageView9);
    // iv.setImageURI(ApplicationConstants.profile_pic_url);
     iv.setImageBitmap(bmpprof);
 btn1.setOnClickListener(new OnClickListener()
   {

  @Override
  public void onClick(View v) {
  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(cameraIntent, CAMERA_REQUEST);
              dialog.dismiss();
          
           }

   });
 
   btn2.setOnClickListener(new OnClickListener()
   {
@Override
public void onClick(View v) {
  
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);
  dialog.dismiss();

}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
        if (requestCode == CAMERA_REQUEST && data!=null) {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    iv1.setImageBitmap(photo);
                  
                    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
                    photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             byte[] b = baos.toByteArray(); 
             String base64String=Base64.encode(b);
            
            ImageUploadingTask task=new ImageUploadingTask(this, base64String);
            task.execute();
          
             
                  
        }else if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
        {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    Bitmap bm = BitmapFactory.decodeFile(picturePath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
   
    String base64String=Base64.encode(b);

    ImageUploadingTask task=new ImageUploadingTask(this, base64String);
         task.execute();
     
}
       }


class ImageUploadingTask extends AsyncTask{

DashboardActivity dActivity;
String base_64bstring;
ProgressDialog pDialog;


public ImageUploadingTask(DashboardActivity  activity,String b64string){

this.dActivity=activity;
this.base_64bstring=b64string; 
}

protected void onPreExecute()
{
super.onPreExecute();
pDialog=ApplicationConstants.getDialog(dActivity);
pDialog.show();
}


@Override
protected String doInBackground(String... params)
{

System.out.println(base_64bstring);
AddEvent event=new AddEvent();
event.UploadPhoto(base_64bstring);
return null;
}

@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
pDialog.dismiss();
}
}

}







Step 2> Create the  class with the name of AddEvent.java and add this method .

AddEvent .java:


public String UploadPhoto(String byteString)
{
String SOAP_ACTION1 = "";
String NAMESPACE = "";
String METHOD_NAME1 = "";
String URL = "";  
String result_="";  
 
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);       

//request.addProperty("b64string", byteString);
//request.addProperty("PhotoName", System.currentTimeMillis()+".jpg");


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
 
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);


androidHttpTransport.call(SOAP_ACTION1, envelope);


SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
 result_=result.getProperty(0).toString();

else
{
 result_="no data found"; 
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.print(result_);
return result_;
}




1 comment: