photo Upload to server using retrofit
public class UserInfoActivity extends BaseActivity {
String TAG = "UserInfoActivity";
ActivityUserInfoBinding binding;
Context context;
private static final int GALLERY_INTENT = 222;
File fileProfile = null;
String user_id = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
binding = DataBindingUtil.setContentView(this, R.layout.activity_user_info);
binding.ivBack.setOnClickListener(view -> onBackPressed());
user_id = Saving.getString(context, ConstantData.LOGIN_ID).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.LOGIN_ID);
Rohit.LOG("UserId", user_id);
String f_name = Saving.getString(context, ConstantData.F_NAME).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.F_NAME);
String l_name = Saving.getString(context, ConstantData.L_NAME).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.L_NAME);
String mobile = Saving.getString(context, ConstantData.MOBILE).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.MOBILE);
String email = Saving.getString(context, ConstantData.EMAIL).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.EMAIL);
String photo = Saving.getString(context, ConstantData.PHOTO).equalsIgnoreCase("") ? "" : Saving.getString(context, ConstantData.PHOTO);
if (!photo.isEmpty()) {
Glide.with(context).load(photo).diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true).into(binding.ivProfile);
}
binding.tvName.setText(!f_name.isEmpty() ? f_name : getString(R.string.app_name));
binding.tvSignUpFName.setText(f_name);
binding.tvSignUpLName.setText(l_name);
binding.tvSignUpMobile.setText(mobile);
binding.tvSignUpEmail.setText(email);
binding.ivEdit.setOnClickListener(view -> {
if (checkAndRequestPermissions()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, GALLERY_INTENT);
}
}
});
binding.tvUpdate.setOnClickListener(view -> {
callUpdate();
});
}
void showLoader() {
binding.tvUpdate.setVisibility(View.GONE);
binding.proLoader.setVisibility(View.VISIBLE);
}
void hideLoader() {
binding.tvUpdate.setVisibility(View.VISIBLE);
binding.proLoader.setVisibility(View.GONE);
}
void callUpdate() {
String fName = binding.tvSignUpFName.getText().toString().trim();
String lName = binding.tvSignUpLName.getText().toString().trim();
String mobile = binding.tvSignUpMobile.getText().toString().trim();
String email = binding.tvSignUpEmail.getText().toString().trim();
if (TextUtils.isEmpty(fName)) {
binding.mFName.requestFocus();
binding.mFName.setError("First Name can not be empty");
} else if (TextUtils.isEmpty(lName)) {
binding.mLName.requestFocus();
binding.mLName.setError("Last Name can not be empty");
} else if (TextUtils.isEmpty(mobile)) {
binding.mMobile.requestFocus();
binding.mMobile.setError("Mobile Number can not be empty");
} else if (TextUtils.isEmpty(email)) {
binding.mEmail.requestFocus();
binding.mEmail.setError("Email can not be empty");
} else {
RequestBody reqUserId = RequestBody.create(MediaType.parse("text/plain"), user_id);
RequestBody reqF_name = RequestBody.create(MediaType.parse("text/plain"), fName);
RequestBody reqL_name = RequestBody.create(MediaType.parse("text/plain"), lName);
RequestBody reqMobile = RequestBody.create(MediaType.parse("text/plain"), mobile);
RequestBody reqEmail = RequestBody.create(MediaType.parse("text/plain"), email);
RequestBody requestFileProfile = fileProfile != null ? RequestBody.create(MediaType.parse("multipart/form-data"), fileProfile) : RequestBody.create(MultipartBody.FORM, "");
MultipartBody.Part bodyProfile = MultipartBody.Part.createFormData("profile_img", ((fileProfile != null) ? fileProfile.getName() : ""), requestFileProfile);
ApiInterface apiInterface = APIClient.getClient().create(ApiInterface.class);
Call<UpdateProfileModel> call = apiInterface.upDateUser(reqUserId, reqF_name, reqL_name, reqMobile, reqEmail, bodyProfile);
Rohit.LOG(TAG, "upDateUser url " + call.request());
showLoader();
call.enqueue(new Callback<UpdateProfileModel>() {
@Override
public void onResponse(Call<UpdateProfileModel> call, Response<UpdateProfileModel> response) {
hideLoader();
if (!response.isSuccessful()) {
Toast.makeText(context, "Contact Admin", Toast.LENGTH_SHORT).show();
Rohit.LOG(TAG, "upDateUser error ");
} else {
UpdateProfileModel model = response.body();
if (model.status.equalsIgnoreCase("200")) {
App.setToast(UserInfoActivity.this, response.body().msg, false);
Saving.setString(context, ConstantData.LOGIN_ID, model.data.get(0).id);
Saving.setString(context, ConstantData.F_NAME, model.data.get(0).first_name);
Saving.setString(context, ConstantData.L_NAME, model.data.get(0).last_name);
Saving.setString(context, ConstantData.MOBILE, model.data.get(0).mobile);
Saving.setString(context, ConstantData.EMAIL, model.data.get(0).email);
Saving.setString(context, ConstantData.PHOTO, model.data.get(0).profile_photo);
} else {
App.setToast(UserInfoActivity.this, response.body().msg, true);
}
}
}
@Override
public void onFailure(Call<UpdateProfileModel> call, Throwable t) {
Rohit.LOG(TAG, "upDateUser list error " + t.getMessage());
hideLoader();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (Activity.RESULT_OK == resultCode) {
if (data != null && GALLERY_INTENT == requestCode) {
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 imgDecodableString = cursor.getString(columnIndex);
cursor.close();
fileProfile = new File(imgDecodableString);
Glide.with(context)
.load(imgDecodableString)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(binding.ivProfile);
} else {
//TODO warn the user the operation failed
}
} else {
//TODO warn the user the action was cancelled
}
}
public boolean checkAndRequestPermissions() {
String str = "android.permission.WRITE_EXTERNAL_STORAGE";
int checkSelfPermission = ContextCompat.checkSelfPermission(this, str);
String str2 = "android.permission.READ_EXTERNAL_STORAGE";
int checkSelfPermission2 = ContextCompat.checkSelfPermission(this, str2);
ArrayList arrayList = new ArrayList();
if (checkSelfPermission != 0) {
arrayList.add(str);
}
if (checkSelfPermission2 != 0) {
arrayList.add(str2);
}
if (arrayList.isEmpty()) {
return true;
}
ActivityCompat.requestPermissions(this, (String[]) arrayList.toArray(new String[arrayList.size()]), 2000);
return false;
}
public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
super.onRequestPermissionsResult(i, strArr, iArr);
if (i == 2000) {
HashMap hashMap = new HashMap();
String str = "android.permission.WRITE_EXTERNAL_STORAGE";
hashMap.put(str, Integer.valueOf(0));
String str2 = "android.permission.READ_EXTERNAL_STORAGE";
hashMap.put(str2, Integer.valueOf(0));
if (iArr.length > 0) {
for (int i2 = 0; i2 < strArr.length; i2++) {
hashMap.put(strArr[i2], Integer.valueOf(iArr[i2]));
}
if (((Integer) hashMap.get(str)).intValue() == 0 && ((Integer) hashMap.get(str2)).intValue() == 0) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, GALLERY_INTENT);
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this, str) || ActivityCompat.shouldShowRequestPermissionRationale(this, str2)) {
showDialogOK("Permission required for this app", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
if (i == -1) {
checkAndRequestPermissions();
}
}
});
} else {
Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_SHORT).show();
}
}
}
}
private void showDialogOK(String str, DialogInterface.OnClickListener onClickListener) {
new AlertDialog.Builder(context).setMessage((CharSequence) str).setPositiveButton((CharSequence) "OK", onClickListener).setNegativeButton((CharSequence) "Cancel", onClickListener).create().show();
}
}
0 Comments
Post a Comment