如何在Android中将“ ImageView”字段设为必选字段?

时间:2019-08-14 02:37:08

标签: android image android-imageview

我的android应用中有一个imageview字段,在onclick上我已赋予它“打开相机”功能,用户可以单击相机中的照片并将其上传到ImageView

  b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(user_image.getDrawable() == null ){

                Toast.makeText(NewCall4.this, "No image uploaded", Toast.LENGTH_SHORT).show();
            }
            else{

                Intent i=new Intent(NewCall4.this,NewCall5.class);
                startActivity(i);
            }

        }
    }
    );

我想要的是,如果我没有在imageview中上传图片,那么它应该将错误/敬酒消息显示为“未上传图片”

3 个答案:

答案 0 :(得分:1)

  • 这是如此简单。当您为相机打开调用Intent时,它将在onActivityResult中提供回调,即仅传递图像的值。并在点击事件中检查它是否为空。
  

示例:-

=> 要打开相机:-

Intent intent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent1, camera);
  • 您将在onActivityResult中得到回调

=> onActivityResult:-(我在这里获得了实际的图像路径)

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;
}
  • 最后,imagepath具有图像的实际路径。

=> 检查图像路径是否为空 :-(如果有)

if (imagepath == null) {
   Toast.makeText(this, "Please Select FileImage", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

嗨,您可以尝试这样的事情

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

和OnResult

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

Bitmap photo = (Bitmap) data.getExtras().get(“data”);
img.setImageBitmap(photo);
 }

答案 2 :(得分:0)

您可以检查Drawable是否附加到ImageView

Imageview imageView = (ImageView)findViewById(R.id.image);
if(imageView.getDrawable() == null){
 //Then Nothing is attached with imageviw
}