强制用户在android中选择一张照片

强制用户在android中选择一张照片,android,Android,在我的android应用程序文件activity\u mai.xml中,我在ImageView中设置了ic\u launcher.png的默认值 <ImageView android:id="@+id/viewImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.05" android:contentDescript

在我的android应用程序文件
activity\u mai.xml
中,我在
ImageView
中设置了
ic\u launcher.png
的默认值

<ImageView
  android:id="@+id/viewImage"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="0.05"
  android:contentDescription="@string/content"
  android:src="@drawable/ic_launcher" 
/>
提前谢谢

编辑

        if (imageview.getDrawable() == getResources().getDrawable(
            R.drawable.ic_launcher)) {
        Toast.makeText(MainActivity.this, "image",
                Toast.LENGTH_SHORT).show();
    }
LogCat

09-26 14:11:10.530: 
D/MainActivity.class(9773): 
myDrawable 1 : android.graphics.drawable.BitmapDrawable@425dc490


09-26 14:11:10.530: D/MainActivity.class(9773): 
myDrawable 2 : android.graphics.drawable.BitmapDrawable@4326f0e0



        Log.d("MainActivity.class", "myDrawable 1 : " + imageview.getDrawable());
        Log.d("MainActivity.class", "myDrawable 2 : "
                + getResources().getDrawable(R.drawable.ic_launcher));

无法使用toString()获取imageView中图像的文件名。以下是API中对toString()的描述:

  • 返回一个字符串,其中包含此对象的简明易读描述
但这并不是指文件名。如果要从图像获取文件名,在将此图像设置为视图时,最好将文件名设置为标记,如下所示:

   yourImageView.setTag(yourFileName);
为了给您提供更好的帮助,我们需要查看用户选择图像的位置以及该图像设置到视图的位置。在这个地方,必须有被设置为标签的名称。

试试这个

Drawable myDrawable = yourImageView.getDrawable();
您可以将其与可提取资源进行比较,如

if(yourImageView.getDrawable()==getResources().getDrawable(R.drawable.ic_launcher)){
    messageText.setError("image");
}
else
{
    // complete your work       
}

请尝试本教程,以下是您在Android(Java)中比较两个可绘制对象的方法:

imageview
是imageview对象吗?是imageview对象,那么这是什么
imageview.toString()
?如果(imageview.toString().equals(“ic_launcher.png”)我已经尝试过了,但不起作用,你认为这是怎么回事<代码>如果(imageview.getDrawable()==null)谢谢,但upolad已经启动。。。请阅读我在firts中的编辑问题是否将imageview图像与启动器图像与此编辑代码进行比较?为了验证上载,如果默认图像不是ic_launcher.png,我需要控制。如果图像为ic_launcher.png,则不验证上载,否则验证上载。是。这是真的。但通过这个可提取的比较,它是加入if子句还是else子句?这个比较是否有效?不起作用,因为使用ic_启动器上传是有效的。
if(yourImageView.getDrawable()==getResources().getDrawable(R.drawable.ic_launcher)){
    messageText.setError("image");
}
else
{
    // complete your work       
}