首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置可绘制为imageView

设置可绘制为imageView
EN

Stack Overflow用户
提问于 2012-11-06 11:13:55
回答 3查看 4.7K关注 0票数 0

我目前正在尝试检索一个包的图标,并使用此代码将其设置为imageView。ai是一个可绘制的。

代码语言:javascript
运行
复制
final PackageManager pm = getPackageManager();

try {
        ai = pm.getApplicationIcon(packageName);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Bitmap bitmap = ((BitmapDrawable)ai).getBitmap();

    Log.i("Icons Drawable", ai.toString());
    Log.i("Icons Bitmap", bitmap.toString());

imageView.setImageBitmap(bitmap);

Logcat输出:

代码语言:javascript
运行
复制
    11-06 11:10:22.785: I/Icons Drawable(20017): android.graphics.drawable.BitmapDrawable@416184f0
    11-06 11:10:22.785: I/Icons Bitmap(20017): android.graphics.Bitmap@41618488

11-06 11:10:22.790: E/AndroidRuntime(20017): FATAL EXCEPTION: main
11-06 11:10:22.790: E/AndroidRuntime(20017): java.lang.RuntimeException: Unable to start activity : java.lang.NullPointerException
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1973)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1999)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.os.Looper.loop(Looper.java:137)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread.main(ActivityThread.java:4513)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at java.lang.reflect.Method.invokeNative(Native Method)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at java.lang.reflect.Method.invoke(Method.java:511)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:974)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:741)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at dalvik.system.NativeStart.main(Native Method)
11-06 11:10:22.790: E/AndroidRuntime(20017): Caused by: java.lang.NullPointerException
11-06 11:10:22.790: E/AndroidRuntime(20017):    at com.analyze.project.MalwareAlertDialog.onCreate(MalwareAlertDialog.java:98)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.Activity.performCreate(Activity.java:4465)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
11-06 11:10:22.790: E/AndroidRuntime(20017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
11-06 11:10:22.790: E/AndroidRuntime(20017):    ... 11 more

即使我尝试直接将可绘制设置为imageView,它也不起作用

代码语言:javascript
运行
复制
imageView.setImageDrawable(ai);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-11-06 11:38:58

尝尝这个

代码语言:javascript
运行
复制
PackageManager pm = getPackageManager();
ApplicationInfo info=pm.getApplicationInfo(packageName,PackageManager.GET_META_DATA);
imageView.setImageDrawable(info.loadIcon(pm));
票数 3
EN

Stack Overflow用户

发布于 2012-11-06 11:33:03

我建议你使用输入流,这是一个例子

代码语言:javascript
运行
复制
InputStream floorIs = context.getResources().openRawResource(R.drawable.floor_texture);

InputStream wallIs =context.getResources().openRawResource(R.drawable.wall_mayu_texture);

InputStream wallIs2 = context.getResources().openRawResource(R.drawable.wall_buzoo_texture);

InputStream wallIs3 = context.getResources().openRawResource(R.drawable.wall_niko_texture);

InputStream wallWindowIs =context.getResources().openRawResource(R.drawable.wall_window_texture);

InputStream ceilingIs = context.getResources().openRawResource(R.drawable.ceiling_texture);

Bitmap bitmaps[] = new Bitmap[6];
bitmaps[0] = null;
bitmaps[1] = null;
bitmaps[2] = null;
bitmaps[3] = null;
bitmaps[4] = null;
bitmaps[5] = null;
            try{
                bitmaps[0] = BitmapFactory.decodeStream(floorIs);
                bitmaps[1] = BitmapFactory.decodeStream(wallIs);
                bitmaps[2] = BitmapFactory.decodeStream(wallWindowIs);
                bitmaps[3] = BitmapFactory.decodeStream(ceilingIs);
                bitmaps[4] = BitmapFactory.decodeStream(wallIs2);
                bitmaps[5] = BitmapFactory.decodeStream(wallIs3);
            }
            finally{
                try{
                    floorIs.close();
                    floorIs = null;
                    wallIs.close();
                    wallIs = null;
                    wallIs2.close();
                    wallIs2 = null;
                    wallIs3.close();
                    wallIs3 = null;
                    wallWindowIs.close();
                    wallWindowIs = null;
                    ceilingIs.close();
                    ceilingIs = null;
                }
                catch(IOException e){}
            }

在此之后,尝试将位图加载到您在使用之前定义的图像视图

代码语言:javascript
运行
复制
imageView.setImageBitmap(bitmap[0]);
票数 0
EN

Stack Overflow用户

发布于 2012-11-06 11:47:40

这可能是一个有点天真的问题,但由于这是在onCreate()回调中发生的,并且该ImageView将出现空指针异常,您是否忘记调用setContentView来扩展您的布局?您的布局可能从未创建过,这意味着您试图绘制的视图也不存在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13243819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档