How to make a "ImageView" field as mandatory in android?

by akshay dhone   Last Updated August 14, 2019 04:26 AM

I have a imageview field in my android app, onclick of the same I have given a "open camera" functionality and the user can click the photo from camera and it gets uploaded to ImageView

What I want is, if I am not uploading the image in the imageview then it should give the error/toast message as "Image not uploaded"



Answers 2


Hi you can try something like this

 Imageview img = (ImageView)findViewById(R.id.image){

    img .setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, id);
                }
    });

And OnResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

Bitmap photo = (Bitmap) data.getExtras().get(“data”);
img.setImageBitmap(photo);
 }
KIRAN CSN
KIRAN CSN
August 14, 2019 03:05 AM

  • It is so simple. When You call Intent for camera open it gives callback in onActivityResult in that just pass the value of the image. And check it is null or not in the click event.

Example :-

=> To Open Camera :-

Intent intent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent1, camera);
  • You will get a callback in onActivityResult

=> onActivityResult :- ( I am getting actual Image path here)

if (resultCode == RESULT_OK && requestCode == camera) {
    Bitmap photo = (Bitmap) data.getExtras().get("data");
    addprofile.setImageBitmap(photo); // set image to imageview
    Uri tempUri = getImageUri(getApplicationContext(), photo);
    File finalfile = new File(getrealpathfromuri(tempUri));
    imagepath = finalfile.toString(); // imagepath is a global variable
}

private Uri getImageUri(Context applicationContext, Bitmap photo) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(),photo,"Title",null);
    return Uri.parse(path);
}

private String getrealpathfromuri(Uri tempUri) {
    String path = "";
    if(getContentResolver() != null){
        Cursor cursor = getContentResolver().query(tempUri, null, null, null, null);
        if(cursor != null){
            cursor.moveToFirst();
            int idk = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            path = cursor.getString(idk);
            cursor.close();
        }
    }
    return path;
}
  • Finally imagepath has an actual path of the image.

=> Check imagepath null or not :- (in event)

if (imagepath == null) {
   Toast.makeText(this, "Please Select FileImage", Toast.LENGTH_SHORT).show();
}
Bhaven Shah
Bhaven Shah
August 14, 2019 04:17 AM

Related Questions


Updated July 04, 2017 17:26 PM

Updated October 28, 2018 04:26 AM

Updated August 14, 2019 02:26 AM

Updated May 26, 2019 13:26 PM

Updated June 01, 2017 05:26 AM