如何检查ImageView是否包含位图? [英] How to check if ImageView contains Bitmap or not?

查看:191
本文介绍了如何检查ImageView是否包含位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现如果 ImageView 有位图,那么它应该将图像从imageview保存到内部存储器,否则在应用程序的内部存储器中设置另一个位图。
这里是代码:_

I am implementing if a ImageView has bitmap then it should save the image from imageview to internal memory ,otherwise set another bitmap in internal memory of application. here is code:_

 croppedImage = cropImageView.getCroppedImage();
                 croppedImageView = (ImageView) findViewById(R.id.croppedImageView);
                croppedImageView.setImageBitmap(croppedImage);@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_save:
            counter++;
            if(croppedImageView.getDrawable() != null)
            {
                System.out.println("nullllllllllllll");

                try {
                    Bitmap photo = ((BitmapDrawable)croppedImageView.getDrawable()).getBitmap();
                    FileOutputStream mFileOutStream1 = openFileOutput("IMG" + counter + ".png", Context.MODE_PRIVATE);
                    photo.compress(CompressFormat.JPEG, 100, mFileOutStream1);} 
                catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();}
                }else{
                  System.out.println("notttttnullllllllllllll");
                  try {
                     FileOutputStream mFileOutStream1 = openFileOutput("IMG" + counter + ".png", Context.MODE_PRIVATE);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, mFileOutStream1);
                  } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                  }

                }
Editor editor = def.edit();
            editor.putInt("value", counter);
            editor.commit();
    break;

        default:
            break;
    }
    }


推荐答案

你可以按如下方式检查:

You can check it as follows:

boolean hasDrawable = (croppedImageView.getDrawable() != null);
if(hasDrawable) {
    // imageView has image in it
}
else {
    // no image assigned to image view
}

只检查位图值如下:

if(bitmap == null) {
    // set the toast for select image
} else {
    uploadImageToServer();
}

这篇关于如何检查ImageView是否包含位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆