iamRaja iamRaja - 2 months ago 10
Android Question

how to avoid the null value for bitmap images

I have made an program to upload image , it has two buttons one to pick image fom gallary and another is to upload it to my Database but if i click the upload button without choosing the image the i face null value ,how can i avoid it.?
any one help me.

Here is my upload code:

public void InsertImage() {
class UpdateEmployee extends AsyncTask<Void, Void, String> {
String image = getStringImage(bitmap);


ProgressDialog loading;

@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(Settings.this, "Updating...", "Wait...", false, false);
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(Settings.this, s, Toast.LENGTH_LONG).show();
}

@Override
protected String doInBackground(Void... params) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put(Config.KEY_JOB_USER_ID, UserDetails.user_id);
hashMap.put(Config.TAG_IMAGE, image);

RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(Config.URL_UPDATE_EMP, hashMap);
return s;
}
}
UpdateEmployee ue = new UpdateEmployee();
ue.execute();
}


here is getStingImage method:

public String getStringImage(Bitmap bmp) {

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] imageBytes = baos.toByteArray();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
}

Answer

Just change:

UpdateEmployee ue = new UpdateEmployee();
ue.execute();

to:

if (bitmap != null) {

UpdateEmployee ue = new UpdateEmployee();
ue.execute();

}

No?