user6644103
user6644103

Reputation:

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);
}

Upvotes: 2

Views: 342

Answers (3)

yong.k
yong.k

Reputation: 678

You can do it like this,

@Override
    protected String doInBackground(Void... params) {
        if(!image.equals("")){
        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;
        }
    }

Upvotes: 3

Vyacheslav
Vyacheslav

Reputation: 27221

You wrote incorrect code. Class definition inside function.

    class UpdateEmployee extends AsyncTask<Void, Void, String> {



        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();
if (s != null)
            Toast.makeText(Settings.this, s, Toast.LENGTH_LONG).show();
else 
Toast.makeText(Settings.this, "ERROR", Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(Void... params) {
/// move here to prevent UI thread block
if (bitmap == null)
return null;
String image = getStringImage(bitmap);
if (image == null)
return null;
            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;
        }
    }


 public void InsertImage() {
if (bitmap != null) {
        UpdateEmployee ue = new UpdateEmployee();
        ue.execute();
}
    }

Upvotes: 0

Nerdy Bunz
Nerdy Bunz

Reputation: 7497

Just change:

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

to:

if (bitmap != null) {

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

}

No?

Upvotes: 1

Related Questions