如何检查imageview是否为空或者在android中以编程方式创建的imageview

时间:2016-03-10 04:34:05

标签: android

我以编程方式创建了图像视图,并且需要检查图像视图是否为空。

getdrawable(),getHeigt()getwidth()方法,但它对我不起作用。 Imageview的创建方式如下:

 ImageView iv = new ImageView(this);

代码:

public void dynamicTable(String alist [])抛出NullPointerException {

    try {
        TableLayout tl = (TableLayout) findViewById(R.id.main_table1);
        tl.setBackgroundColor(Color.GRAY);
        sizelen = alist.length;
        cam = new Button[sizelen];
        iv = new ImageView[sizelen];
        int k;

        for (k = 0; k < sizelen; k++) {
            try {
                final int j = k;

                TableRow tr_head = new TableRow(this);
                TableLayout.LayoutParams rowparams = new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT);
                int leftMargin = 2;
                int topMargin = 1;
                int rightMargin = 2;
                int bottomMargin = 1;

                rowparams.setMargins(leftMargin, topMargin, rightMargin,
                        bottomMargin);
                rowparams.weight = (float) 0.5;
                tr_head.setLayoutParams(rowparams);

                // tr_head.setId(10);

                tr_head.setBackgroundColor(Color.WHITE);
                /*
                 * tr_head.setLayoutParams(new LayoutParams(
                 * LayoutParams.FILL_PARENT LayoutParams.WRAP_CONTENT));
                 */

                cam[k] = new Button(this);
                cam[k].setText(alist[k]);
                cam[k].setTextColor(Color.WHITE);
                cam[k].setBackgroundColor(Color.parseColor("#ffab00"));
                cam[k].setClickable(true);
                cam[k].setFocusable(true);
                cam[k].setGravity(Gravity.LEFT);
                cam[k].setMaxLines(2);
                cam[k].setPadding(10, 10, -10, 10);
                cam[k].setAllCaps(false);
                cam[k].setTextSize(13);
                cam[k].setLayoutParams(new TableRow.LayoutParams(85,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                // cam[i].setWidth(150);
                // cam[i].setEms(0);

                // cam[i].setGravity(25);
                // cam[i](Gravity.LEFT);

                // cam[i].setLayoutParams(params);
                cam[k].setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cam[j].setBackgroundColor(Color.GRAY);
                        camera(j);
                    }
                });

                tr_head.addView(cam[k]); // add the column to the table row
                                            // here

                iv[k] = new ImageView(this);
                iv[k].setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT));
                iv[k].setPadding(0, 7, 10, 0);
                iv[k].setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        imageviewOnclick(iv[j]);
                    }
                });
                tr_head.addView(iv[k]); // dd the column to the table row
                                        // here
                tl.addView(tr_head, rowparams);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

这些方法会拍摄手机中的照片并将其显示在ImageView中,我猜你可以在代码中使用它来查看照片是否显示。

    public void DisplayPhotoPreview(){
    DisplayPhotoPreview = (Button)findViewById(R.id.UploadPhotoPreviewBtn);//Finds the button in design and put it into a button variable.
    DisplayPhotoPreview.setOnClickListener(//Listens for a button click.
            new View.OnClickListener() {//Creates a new click listener.
                @Override
                public void onClick(View v) {//does what ever code is in here when the button is clicked
                    switch (v.getId()) {//switches to the case if that .id. is clicked.
                        case R.id.UploadPhotoPreviewBtn://Does what ever the code is in the case if the upload button is clicked.
                            //I open up an intent variable and through different commands and I open up my gallery on my phone and i cant get
                            //out of the gallery until i choose a photo. Then I save the photo in the intent and call the onActivityResult() method which displays the image.
                            //Also a intent is how two activities can send data.
                            Intent ChoosePhotoFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            startActivityForResult(ChoosePhotoFromGallery, RESULT_LOAD_IMAGE);//Stores the image into the variable.
                            break;
                    }


                }
            }
    );
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //so the if statement checks if the image came through, double checks if the result is okay and triple checks to see if the data is not null.
    if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
        //gets the address of the image/data !!!!!!ALSO CAN HOLD THE ADDRESS FOR THE SERVER!!!!!!.
        Uri Image = data.getData();
        ItemPhotoPreview = (ImageView)findViewById(R.id.ItemPhotoPreviewImageView);
        //Sets the image into the variable and displays it
        ItemPhotoPreview.setImageURI(Image);
    }
}