如何避免位图图像的空值

时间:2016-09-13 03:29:43

标签: android bitmap

我已经制作了一个上传图片的程序,它有两个按钮,一个用于选择图像,另一个是将其上传到我的数据库但是如果我点击上传按钮而不选择图像我面对空值,怎么能我避免它。 任何人帮助我。

这是我的上传代码:

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

这里是getStingImage方法:

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

3 个答案:

答案 0 :(得分:3)

你可以这样做,

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

答案 1 :(得分:1)

只需改变:

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

为:

if (bitmap != null) {

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

}

没有

答案 2 :(得分:0)

您编写了错误的代码。函数内部的类定义。

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