Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UpdateProfileActivity extends AppCompatActivity {
- Toolbar toolbar;
- ImageView imgProfile;
- Button btnSaveProfile,btnMyadd;
- EditText edtName,edtEmail,edtNumber,edtWebsite;
- TextView txtname,txtEmail,txtNumber,txtWebsite;
- ProgressDialog progressBar;
- String name,email,number,website;
- private String selectedImagePath = "";
- final private int PICK_IMAGE = 1;
- final private int CAPTURE_IMAGE = 2;
- private String imgPath;
- File file;
- boolean isphotoselected = false;
- private SharedPreferences pref;
- Typeface typeface;
- String advertId,advertUserName,advertPassword,advertEmail,advertPhone,advertSite,advertImage;
- boolean AddFlag= false;
- MarshMallowPermission marshMallowPermission;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_updateprofile);
- toolbar = (Toolbar) findViewById(R.id.toolbar);
- typeface = Typeface.createFromAsset(getAssets(), "fonts/GandhiSerif-Bold.otf");
- if (toolbar != null) {
- toolbar.setTitle("Profile Update");
- setSupportActionBar(toolbar);
- getSupportActionBar().setHomeButtonEnabled(true);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true);
- }
- getActionBarTextView();
- marshMallowPermission = new MarshMallowPermission(this);
- if (!marshMallowPermission.checkPermissionForExternalStorage()) {
- marshMallowPermission.requestPermissionForExternalStorage();
- }
- if (!marshMallowPermission.checkPermissionForCamera()) {
- marshMallowPermission.requestPermissionForCamera();
- }
- if (!marshMallowPermission.checkPermissionForExternalStorage()) {
- marshMallowPermission.requestPermissionForExternalStorage();
- }
- imgProfile = (ImageView)findViewById(R.id.imgProfile);
- btnMyadd = (Button)findViewById(R.id.btnMyadd);
- btnSaveProfile=(Button)findViewById(R.id.btnSaveprofile);
- edtEmail = (EditText)findViewById(R.id.edtEmailid);
- edtName = (EditText)findViewById(R.id.edtName);
- edtNumber = (EditText)findViewById(R.id.edtphone);
- edtWebsite= (EditText)findViewById(R.id.edtwebsite);
- txtEmail = (TextView)findViewById(R.id.txtEmail);
- txtname = (TextView)findViewById(R.id.txtName);
- txtNumber = (TextView)findViewById(R.id.txtPhone);
- txtWebsite= (TextView)findViewById(R.id.txtWeb);
- btnMyadd.setTypeface(typeface);
- btnSaveProfile.setTypeface(typeface);
- edtEmail.setTypeface(typeface);
- edtName.setTypeface(typeface);
- edtNumber.setTypeface(typeface);
- edtWebsite.setTypeface(typeface);
- txtEmail.setTypeface(typeface);
- txtname.setTypeface(typeface);
- txtNumber.setTypeface(typeface);
- txtWebsite.setTypeface(typeface);
- pref = getSharedPreferences("loginpreference", MODE_PRIVATE);
- imgProfile.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- startDialog();
- }
- });
- btnSaveProfile.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- name = edtName.getText().toString();
- number = edtNumber.getText().toString();
- email = edtEmail.getText().toString();
- website= edtWebsite.getText().toString();
- boolean flag = emailValidator(email);
- if(name.equalsIgnoreCase("")){
- edtName.setError("Please Enter Name");
- }else if(number.equalsIgnoreCase("")){
- edtNumber.setError("Please Enter Contact Number");
- }else if(flag == false){
- edtEmail.setError("Please Enter Email");
- }else if(website.equalsIgnoreCase("")){
- edtWebsite.setError("Please Enter Website");
- }else if(selectedImagePath.equalsIgnoreCase("") || selectedImagePath==null){
- Toast.makeText(getApplicationContext(), "Please Select Profile Picture", Toast.LENGTH_SHORT).show();
- }else {
- new UpdateProfileTask().execute();
- }
- }
- });
- btnMyadd.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if(!AddFlag){
- Toast.makeText(UpdateProfileActivity.this, "Please Update Your Profile", Toast.LENGTH_SHORT).show();
- }else{
- AddFlag = true;
- Intent intent =new Intent(UpdateProfileActivity.this, MyAdsActivity.class);
- intent.putExtra("advertId", advertId);
- startActivity(intent);
- }
- }
- });
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if(id==android.R.id.home)
- {
- onBackPressed();
- }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public void onBackPressed() {
- // TODO Auto-generated method stub
- super.onBackPressed();
- }
- public boolean emailValidator(String s)
- {
- return Pattern.compile("^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$").matcher(s).matches();
- }
- private TextView getActionBarTextView() {
- TextView titleTextView = null;
- try {
- Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
- f.setAccessible(true);
- titleTextView = (TextView)f.get(toolbar);
- titleTextView.setTypeface(typeface);
- } catch (NoSuchFieldException e) {
- } catch (IllegalAccessException e) {
- }
- return titleTextView;
- }
- class UpdateProfileTask extends AsyncTask<Void, Void, Void>
- {
- String jsonStr = null;
- CustomProgressDialog cd= new CustomProgressDialog();
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- cd.showdialog(UpdateProfileActivity.this, "Loading...");
- }
- @Override
- protected Void doInBackground(Void... arg0) {
- jsonStr = uploadFile();
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- cd.dismissdialog();
- if (jsonStr != null) {
- try {
- JSONObject jsonObj = new JSONObject(jsonStr);
- String responce = jsonObj.getString(Constants.LOGIN_TAG);
- String msg = jsonObj.getString(Constants.REG_TAG);
- if(responce.equalsIgnoreCase("success")){
- JSONArray jArray = jsonObj.getJSONArray("data");
- if(jArray.length()>0){
- JSONObject j1 = jArray.getJSONObject(0);
- advertId = j1.getString("advertId");
- advertUserName = j1.getString("advertUserName");
- advertPassword = j1.getString("advertPassword");
- advertEmail = j1.getString("advertEmail");
- advertPhone = j1.getString("advertPhone");
- advertSite = j1.getString("advertSite");
- advertImage = j1.getString("advertImage");
- AddFlag = true;
- }
- Toast.makeText(UpdateProfileActivity.this, msg, Toast.LENGTH_SHORT).show();
- }else{
- Toast.makeText(UpdateProfileActivity.this, msg, Toast.LENGTH_SHORT).show();
- }
- edtEmail.setText("");
- edtName.setText("");
- edtNumber.setText("");
- edtWebsite.setText("");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- } else {
- Log.e("ServiceHandler", "Couldn't get any data from the url");
- }
- }
- }
- @SuppressWarnings("deprecation")
- private String uploadFile() {
- String responseString = null;
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(Constants.WEBURL);
- try {
- AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new AndroidMultiPartEntity.ProgressListener() {
- @Override
- public void transferred(long num) {
- }
- });
- if(isphotoselected){
- file = new File(selectedImagePath);
- //entity.addPart("userfile",selectedImagePath.toString();
- entity.addPart("avtar", new FileBody(file));
- //entity.addPart("filesArray[]",new FileBody(file));
- isphotoselected = false;
- }
- String advartid = pref.getString("advertId", "");
- entity.addPart("action", new StringBody("updateAccount"));
- entity.addPart("advertId", new StringBody(advartid));
- entity.addPart("username", new StringBody(name));
- entity.addPart("email", new StringBody(email));
- entity.addPart("phone", new StringBody(number));
- entity.addPart("site", new StringBody(website));
- httppost.setEntity(entity);
- // Making server call
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity r_entity = response.getEntity();
- int statusCode = response.getStatusLine().getStatusCode();
- if (statusCode == 200) {
- // Server response
- responseString = EntityUtils.toString(r_entity);
- } else {
- responseString = "Error occurred! Http Status Code: "+ statusCode;
- }
- } catch (ClientProtocolException e) {
- responseString = e.toString();
- } catch (IOException e) {
- responseString = e.toString();
- }
- return responseString;
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode,
- Intent data) {
- if (resultCode != Activity.RESULT_CANCELED) {
- if (requestCode == PICK_IMAGE) {
- isphotoselected = true;
- selectedImagePath = getAbsolutePath(data.getData());
- imgProfile.setImageBitmap(decodeFile(selectedImagePath));
- } else if (requestCode == CAPTURE_IMAGE) {
- isphotoselected = true;
- selectedImagePath = getImagePath();
- imgProfile.setImageBitmap(decodeFile(selectedImagePath));
- } else {
- super.onActivityResult(requestCode, resultCode,
- data);
- }
- }
- }
- private void startDialog()
- {
- android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
- builder.setTitle("Upload Pictures Option");
- builder.setMessage("How do you want to set your picture?");
- builder.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialoginterface, int i)
- {
- Intent intent = new Intent();
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(Intent.createChooser(intent, ""),PICK_IMAGE);
- }
- });
- builder.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialoginterface, int i)
- {
- final Intent intent = new Intent(
- MediaStore.ACTION_IMAGE_CAPTURE);
- intent.putExtra(MediaStore.EXTRA_OUTPUT,
- setImageUri());
- startActivityForResult(intent, CAPTURE_IMAGE);
- }
- });
- builder.show();
- }
- public Uri setImageUri() {
- // Store image in dcim
- File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
- Uri imgUri = Uri.fromFile(file);
- this.imgPath = file.getAbsolutePath();
- return imgUri;
- }
- public String getImagePath() {
- return imgPath;
- }
- public Bitmap decodeFile(String path) {
- try {
- // Decode image size
- BitmapFactory.Options o = new BitmapFactory.Options();
- o.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(path, o);
- // The new size we want to scale to
- final int REQUIRED_SIZE = 70;
- // Find the correct scale value. It should be the power of
- // 2.
- int scale = 1;
- while (o.outWidth / scale / 2 >= REQUIRED_SIZE
- && o.outHeight / scale / 2 >= REQUIRED_SIZE)
- scale *= 2;
- // Decode with inSampleSize
- BitmapFactory.Options o2 = new BitmapFactory.Options();
- o2.inSampleSize = scale;
- return BitmapFactory.decodeFile(path, o2);
- } catch (Throwable e) {
- e.printStackTrace();
- }
- return null;
- }
- public String getAbsolutePath(Uri uri) {
- String[] projection = { MediaColumns.DATA };
- Cursor cursor = managedQuery(uri, projection, null, null, null);
- if (cursor != null) {
- int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); cursor.moveToFirst();
- return cursor.getString(column_index);
- } else
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement