ImageView setImageDrawable导致绘制空白图像

时间:2015-07-01 20:00:57

标签: android imageview

onCreate()

中的代码
ApplicationInfo app = getPackageManager().getApplicationInfo(getIntent().getStringExtra("app"), 0);
((TextView) findViewById(R.id.textView2)).setText(app.loadLabel(getPackageManager()));
((ImageView) findViewById(R.id.imageView2)).setImageDrawable(app.loadLogo(getPackageManager()));

第三行设置的图像应该绘制应用程序图标,而是绘制空白。请注意,有问题的应用程序不是我的应用程序,而是手机上的应用程序。所以,我已经将ApplicationInfo直接传递给了这个活动,并且通过现在​​的包名,所以我不认为这是问题所在。我还在XML中设置了一个虚拟图像,并对第三行进行了注释,并且该图像正确显示。我做错了什么?

XML

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/imageView2" />

2 个答案:

答案 0 :(得分:0)

在我看来,你选择了非常奇怪的方式设置图像。 您是否尝试过

设置它
int recursiveTest(char* str,int len)
{
    if (len <= 1)
        return 1;
    if (str[0] != str[len-1])
        return 0;
    return recursiveTest(str+1,len-2);
}

int isPalindrome(char* str)
{
    return recursiveTest(str,strlen(str));
}

int main()
{
    char str[100];

    printf("Please Enter a string:\n");
    scanf("%99s",str);

    if (isPalindrome(str))
        printf("The input is a palindrome.\n");
    else
        printf("The input is not a palindrome.\n");

    return 0;
}

((ImageView) findViewById(R.id.imageView2)).setImageResource(R.drawable.your_image_name_here)

答案 1 :(得分:0)

如果您想在图像视图中以编程方式显示“App徽标”

你可以试试这个:

 Drawable icon = null;
        try
        {
            icon = getPackageManager().getApplicationIcon("packagename of your app");
            ((ImageView) findViewById(R.id.imageView2)).setImageDrawable(icon);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }