簡體   English   中英

如果未選擇圖像,則顯示錯誤提示

[英]Show error note if image not selected android

這是我下面的完整代碼。 我正在編碼一個可以從畫廊或任何地方選擇圖像的android應用程序,並通過php將其上傳到Mysql服務器,但是如果未選擇圖像並且按下了上傳按鈕,則應用程序崩潰,因此我想實現顯示錯誤對話框的功能/如果未從圖庫中選擇圖像,則警告對話框已在EditText中實現,但無法在Image中實現。 下面是我的代碼

package com.app.markeet;



public class ActivityPost extends AppCompatActivity implements View.OnClickListener {

    private Button buttonUpload;
    private Button buttonChoose;

    EditText editTitle, editTextPrice, editTextDescription, editTextStatus, editTextAuthorsname, editTextAuthorsphone;
    private ImageView imageView,choiceItemImg,choiceItemImg2,choiceItemImg3,choiceItemImg4;

    public static final String KEY_TITLE = "name";
    public static final String KEY_IMAGE = "image";
    public static final String KEY_PRICE = "price";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_STOCK = "stock";
    public static final String KEY_AUTHORSNAME = "authorsname";
    public static final String KEY_AUTHORSPHONE = "authorsphone";
    public static final String KEY_STATUS = "status";
    public static final String UPLOAD_URL = "http://192.168.0.199/config/upload.php";
    private int PICK_IMAGE_REQUEST = 1;
    public static final int CREATE_POST_IMG = 5;
    private Bitmap bitmap;
    private Uri outputFileUri;
    public static final int SELECT_POST_IMG = 3;
    public static final String APP_TEMP_FOLDER = "kobobay";

    String[] categories = { "Animals and Pets", "Electronics and Video",
            "Fashion and Beauty", "Home,  Furniture  and Garden", "Mobile  Phone  and Tablets", "PC, Laptop and Accessories",
            "Jobs and Services", "Real Estate and Roommate", "Hobbles, Art and Sport", "Books and Tutorial",
            "Vehicles", "Other"};
    int[] catevalue = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    Spinner spinner1;
    TextView textView1;

    private Toolbar toolbar;
    private ActionBar actionBar;

    private String selectedPostImg = "";
    ImageView mChoiceItemImg, mLocationDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle("");

        buttonUpload = (Button) findViewById(R.id.buttonUpload);
        buttonChoose = (Button) findViewById(R.id.buttonChooseImage);

        editTitle = (EditText)findViewById(R.id.editTitle);
        editTextPrice = (EditText)findViewById(R.id.editPrice);
        editTextDescription = (EditText)findViewById(R.id.editDescription);
        editTextStatus = (EditText)findViewById(R.id.editStatus);
        editTextAuthorsname = (EditText)findViewById(R.id.editName);
        editTextAuthorsphone = (EditText)findViewById(R.id.editPhone);
        imageView = (ImageView) findViewById(R.id.imageView);

        textView1 = (TextView)findViewById(R.id.text1);
        spinner1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter1 =
                new ArrayAdapter<String>(ActivityPost.this,
                        android.R.layout.simple_spinner_item, categories);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);
        spinner1.setOnItemSelectedListener(onItemSelectedListener1);

        buttonChoose.setOnClickListener(this);
        buttonUpload.setOnClickListener(this);

        choiceItemImg = (ImageView) findViewById(R.id.choiceItemImg);
        choiceItemImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showFileChooser();
            }
        });

        mChoiceItemImg = (ImageView) findViewById(R.id.choiceItemImg2);

        mChoiceItemImg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (selectedPostImg.length() == 0) {

                    imageFromGallery();

                } else {

                    AlertDialog.Builder alertDialog = new AlertDialog.Builder(ActivityPost.this);
                    alertDialog.setTitle(getText(R.string.action_remove));

                    alertDialog.setMessage(getText(R.string.label_delete_img));
                    alertDialog.setCancelable(true);

                    alertDialog.setNeutralButton(getText(R.string.action_cancel), new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            dialog.cancel();
                        }
                    });

                    alertDialog.setPositiveButton(getText(R.string.action_remove), new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {

                            mChoiceItemImg.setImageResource(R.drawable.ic_camera);
                            selectedPostImg = "";
                            dialog.cancel();
                        }
                    });

                    alertDialog.show();
                }
            }
        });

        String URL = Constant.WEB_URL + "get/user_info.php";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonResponse= new JSONObject(response);
                            //txtName.setText(jsonResponse.getString("fullname"));
                            editTextAuthorsname.setText(jsonResponse.getString("login"));
                            //txtEmail.setText(jsonResponse.getString("email"));
                            editTextAuthorsphone.setText(jsonResponse.getString("phone"));// you need to create this textView txtPoints.
                        } catch (Throwable t) {
                            Log.e("onResponse", "The response: " + response + " seems to not be json formatted.");
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(ActivityPost.this,error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();
                params.put("username",ThisApplication.getInstance().getUsername());
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);

    }

    AdapterView.OnItemSelectedListener onItemSelectedListener1 =
            new AdapterView.OnItemSelectedListener(){

                @Override
                public void onItemSelected(AdapterView<?> parent, View view,
                                           int position, long id) {
                    String s1 = String.valueOf(catevalue[position]);
                    textView1.setText(s1);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {}

            };

    private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }


    public void imageFromGallery() {

        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(intent, getText(R.string.label_select_img)), SELECT_POST_IMG);
    }

    public void imageFromCamera() {

        try {

            File root = new File(Environment.getExternalStorageDirectory(), APP_TEMP_FOLDER);

            if (!root.exists()) {

                root.mkdirs();
            }

            File sdImageMainDirectory = new File(root, "post.jpg");
            outputFileUri = Uri.fromFile(sdImageMainDirectory);

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, CREATE_POST_IMG);

        } catch (Exception e) {

            Toast.makeText(ActivityPost.this, "Error occured. Please try again later.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                choiceItemImg.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (requestCode == SELECT_POST_IMG && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                choiceItemImg2.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public String getStringImage(Bitmap bmp){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }


    public void uploadImage(){
        final String GetAuthorsname = editTextAuthorsname.getText().toString();
        final String GetAuthorsphone = editTextAuthorsphone.getText().toString();
        final String GetTitle = editTitle.getText().toString().trim();
        final String image = getStringImage(bitmap);
        final String GetPrice = editTextPrice.getText().toString();
        final String GetStock = textView1.getText().toString();
        final String GetDescription = editTextDescription.getText().toString();
        final String GetStatus = editTextStatus.getText().toString();

        class UploadImage extends AsyncTask<Void,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(ActivityPost.this,"Please wait...","uploading",false,false);
            }

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

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                HashMap<String,String> param = new HashMap<String,String>();
                param.put(KEY_TITLE,GetTitle);
                param.put(KEY_IMAGE,image);
                param.put(KEY_PRICE,GetPrice);
                param.put(KEY_STOCK,GetStock);
                param.put(KEY_DESCRIPTION,GetDescription);
                param.put(KEY_STATUS,GetStatus);
                param.put(KEY_AUTHORSNAME,GetAuthorsname);
                param.put(KEY_AUTHORSPHONE,GetAuthorsphone);
                String result = rh.sendPostRequest(UPLOAD_URL, param);
                return result;
            }
        }
        UploadImage u = new UploadImage();
        u.execute();
    }

    public Boolean checkPicture() {

        String image = getStringImage(bitmap);

        String GetPrice = editTextPrice.getText().toString();
        String GetDescription = editTextDescription.getText().toString();
        String GetStatus = editTextStatus.getText().toString();

        editTitle.setError(null);
        editTextPrice.setError(null);
        editTextDescription.setError(null);
        editTextStatus.setError(null);


        if (image.length() == 0) {

            editTitle.setError(getString(R.string.error_field_empty));

            return false;

        } if (GetPrice.length() == 0) {

            editTextPrice.setError(getString(R.string.error_field_empty));
            return false;

        } if (GetDescription.length() == 0) {

            editTextDescription.setError(getString(R.string.error_field_empty));

            return false;

        } if (GetStatus.length() == 0) {

            editTextStatus.setError(getString(R.string.error_field_empty));

            return false;
        }

        return  true;
    }

    public Boolean checkDesc() {

        String GetDescription = editTextDescription.getText().toString();
        editTextDescription.setError(null);

        if (GetDescription.length() == 0) {

            editTextDescription.setError(getString(R.string.error_field_empty));

            return false;
        }

        return  true;
    }

    public Boolean checkProductName() {

        String GetTitle = editTitle.getText().toString();

        editTitle.setError(null);

        if (GetTitle.length() == 0) {

            editTitle.setError(getString(R.string.error_field_empty));

            return false;
        }

        return  true;
    }

    public Boolean checkCategory() {

        String GetStock = textView1.getText().toString();

        textView1.setError(null);

        if (GetStock.length() == 0) {

            editTitle.setError(getString(R.string.error_field_empty));

            return false;
        }

        return  true;
    }

    @Override
    public void onClick(View v) {
        if(v == buttonChoose){
            showFileChooser();
            //imageFromGallery();
        }
        if(v == buttonUpload){

            // checkUsername();
            if (!ThisApplication.getInstance().isConnected()) {

                Toast.makeText(getApplicationContext(), R.string.msg_network_error, Toast.LENGTH_SHORT).show();

            } else if (!checkProductName() || !checkDesc() || !checkCategory()) {


            } else {

                uploadImage();
            }

        }
    }

    @Override
    public void onBackPressed(){

        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_new_item, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.sync:
                Intent intent = getIntent();
                finish();
                startActivity(intent);
                return true;
            case R.id.action_post:
                uploadImage();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

只需復制此代碼並粘貼

package com.app.markeet;



public class ActivityPost extends AppCompatActivity implements View.OnClickListener {

private Button buttonUpload;
private Button buttonChoose;

EditText editTitle, editTextPrice, editTextDescription, editTextStatus, editTextAuthorsname, editTextAuthorsphone;
private ImageView imageView,choiceItemImg,choiceItemImg2,choiceItemImg3,choiceItemImg4;

public static final String KEY_TITLE = "name";
public static final String KEY_IMAGE = "image";
public static final String KEY_PRICE = "price";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_STOCK = "stock";
public static final String KEY_AUTHORSNAME = "authorsname";
public static final String KEY_AUTHORSPHONE = "authorsphone";
public static final String KEY_STATUS = "status";
public static final String UPLOAD_URL = "http://192.168.0.199/config/upload.php";
private int PICK_IMAGE_REQUEST = 1;
public static final int CREATE_POST_IMG = 5;
private Bitmap bitmap;
private Uri outputFileUri;
public static final int SELECT_POST_IMG = 3;
public static final String APP_TEMP_FOLDER = "kobobay";

String[] categories = { "Animals and Pets", "Electronics and Video",
        "Fashion and Beauty", "Home,  Furniture  and Garden", "Mobile  Phone  and Tablets", "PC, Laptop and Accessories",
        "Jobs and Services", "Real Estate and Roommate", "Hobbles, Art and Sport", "Books and Tutorial",
        "Vehicles", "Other"};
int[] catevalue = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

Spinner spinner1;
TextView textView1;

private Toolbar toolbar;
private ActionBar actionBar;

private String selectedPostImg = "";
ImageView mChoiceItemImg, mLocationDelete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setTitle("");

    buttonUpload = (Button) findViewById(R.id.buttonUpload);
    buttonChoose = (Button) findViewById(R.id.buttonChooseImage);

    editTitle = (EditText)findViewById(R.id.editTitle);
    editTextPrice = (EditText)findViewById(R.id.editPrice);
    editTextDescription = (EditText)findViewById(R.id.editDescription);
    editTextStatus = (EditText)findViewById(R.id.editStatus);
    editTextAuthorsname = (EditText)findViewById(R.id.editName);
    editTextAuthorsphone = (EditText)findViewById(R.id.editPhone);
    imageView = (ImageView) findViewById(R.id.imageView);

    textView1 = (TextView)findViewById(R.id.text1);
    spinner1 = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 =
            new ArrayAdapter<String>(ActivityPost.this,
                    android.R.layout.simple_spinner_item, categories);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);
    spinner1.setOnItemSelectedListener(onItemSelectedListener1);

    buttonChoose.setOnClickListener(this);
    buttonUpload.setOnClickListener(this);

    choiceItemImg = (ImageView) findViewById(R.id.choiceItemImg);
    choiceItemImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            showFileChooser();
        }
    });

    mChoiceItemImg = (ImageView) findViewById(R.id.choiceItemImg2);

    mChoiceItemImg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (selectedPostImg.length() == 0) {

                imageFromGallery();

            } else {

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(ActivityPost.this);
                alertDialog.setTitle(getText(R.string.action_remove));

                alertDialog.setMessage(getText(R.string.label_delete_img));
                alertDialog.setCancelable(true);

                alertDialog.setNeutralButton(getText(R.string.action_cancel), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                    }
                });

                alertDialog.setPositiveButton(getText(R.string.action_remove), new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        mChoiceItemImg.setImageResource(R.drawable.ic_camera);
                        selectedPostImg = "";
                        dialog.cancel();
                    }
                });

                alertDialog.show();
            }
        }
    });

    String URL = Constant.WEB_URL + "get/user_info.php";

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonResponse= new JSONObject(response);
                        //txtName.setText(jsonResponse.getString("fullname"));
                        editTextAuthorsname.setText(jsonResponse.getString("login"));
                        //txtEmail.setText(jsonResponse.getString("email"));
                        editTextAuthorsphone.setText(jsonResponse.getString("phone"));// you need to create this textView txtPoints.
                    } catch (Throwable t) {
                        Log.e("onResponse", "The response: " + response + " seems to not be json formatted.");
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(ActivityPost.this,error.toString(), Toast.LENGTH_SHORT).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("username",ThisApplication.getInstance().getUsername());
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

AdapterView.OnItemSelectedListener onItemSelectedListener1 =
        new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String s1 = String.valueOf(catevalue[position]);
                textView1.setText(s1);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {}

        };

private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}


public void imageFromGallery() {

    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(Intent.createChooser(intent, getText(R.string.label_select_img)), SELECT_POST_IMG);
}

public void imageFromCamera() {

    try {

        File root = new File(Environment.getExternalStorageDirectory(), APP_TEMP_FOLDER);

        if (!root.exists()) {

            root.mkdirs();
        }

        File sdImageMainDirectory = new File(root, "post.jpg");
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(cameraIntent, CREATE_POST_IMG);

    } catch (Exception e) {

        Toast.makeText(ActivityPost.this, "Error occured. Please try again later.", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            choiceItemImg.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (requestCode == SELECT_POST_IMG && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            choiceItemImg2.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] imageBytes = baos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}


public void uploadImage(){
    final String GetAuthorsname = editTextAuthorsname.getText().toString();
    final String GetAuthorsphone = editTextAuthorsphone.getText().toString();
    final String GetTitle = editTitle.getText().toString().trim();
    final String image = getStringImage(bitmap);
    final String GetPrice = editTextPrice.getText().toString();
    final String GetStock = textView1.getText().toString();
    final String GetDescription = editTextDescription.getText().toString();
    final String GetStatus = editTextStatus.getText().toString();

    class UploadImage extends AsyncTask<Void,Void,String> {
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(ActivityPost.this,"Please wait...","uploading",false,false);
        }

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

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            HashMap<String,String> param = new HashMap<String,String>();
            param.put(KEY_TITLE,GetTitle);
            param.put(KEY_IMAGE,image);
            param.put(KEY_PRICE,GetPrice);
            param.put(KEY_STOCK,GetStock);
            param.put(KEY_DESCRIPTION,GetDescription);
            param.put(KEY_STATUS,GetStatus);
            param.put(KEY_AUTHORSNAME,GetAuthorsname);
            param.put(KEY_AUTHORSPHONE,GetAuthorsphone);
            String result = rh.sendPostRequest(UPLOAD_URL, param);
            return result;
        }
    }
    UploadImage u = new UploadImage();
    u.execute();
}

public Boolean checkPicture() {

    String image = getStringImage(bitmap);

    String GetPrice = editTextPrice.getText().toString();
    String GetDescription = editTextDescription.getText().toString();
    String GetStatus = editTextStatus.getText().toString();

    editTitle.setError(null);
    editTextPrice.setError(null);
    editTextDescription.setError(null);
    editTextStatus.setError(null);


    if (image.length() == 0) {

        editTitle.setError(getString(R.string.error_field_empty));

        return false;

    } if (GetPrice.length() == 0) {

        editTextPrice.setError(getString(R.string.error_field_empty));
        return false;

    } if (GetDescription.length() == 0) {

        editTextDescription.setError(getString(R.string.error_field_empty));

        return false;

    } if (GetStatus.length() == 0) {

        editTextStatus.setError(getString(R.string.error_field_empty));

        return false;
    }

    return  true;
}

public Boolean checkDesc() {

    String GetDescription = editTextDescription.getText().toString();
    editTextDescription.setError(null);

    if (GetDescription.length() == 0) {

        editTextDescription.setError(getString(R.string.error_field_empty));

        return false;
    }

    return  true;
}

public Boolean checkProductName() {

    String GetTitle = editTitle.getText().toString();

    editTitle.setError(null);

    if (GetTitle.length() == 0) {

        editTitle.setError(getString(R.string.error_field_empty));

        return false;
    }

    return  true;
}

public Boolean checkCategory() {

    String GetStock = textView1.getText().toString();

    textView1.setError(null);

    if (GetStock.length() == 0) {

        editTitle.setError(getString(R.string.error_field_empty));

        return false;
    }

    return  true;
}

@Override
public void onClick(View v) {
    if(v == buttonChoose){
        showFileChooser();
        //imageFromGallery();
    }
    if(v == buttonUpload){

        // checkUsername();
        if (!ThisApplication.getInstance().isConnected()) {

            Toast.makeText(getApplicationContext(), R.string.msg_network_error, Toast.LENGTH_SHORT).show();

        } else if (!checkProductName() || !checkDesc() || !checkCategory()) {


        } else {

            uploadImage();
        }

    }
}

@Override
public void onBackPressed(){

    finish();
}

@Override
public void onDestroy() {
    super.onDestroy();
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_new_item, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.sync:
            Intent intent = getIntent();
            finish();
            startActivity(intent);
            return true;
        case R.id.action_post:
            uploadImage();
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM