java - 为什么 Bitmap.getConfig() 返回 null?

标签 java android android-emulator

我有一些 XML 布局生成的 ImageView,我想复制我在下面的 LinearLayout 中单击的图像。

我已将跟随事件分配给所有 ImageViewonClick 事件:

public void onClick(View v) {
    // Take layout where i want to put my copy-image
    LinearLayout savingLayout = (LinearLayout)findViewById(R.id.linearSaved);

    //Create a new image
    ImageView savedImage = new ImageView(savingLayout.getContext());
    //Take the bitmap from the object i clicked
    Bitmap b = ((BitmapDrawable)((ImageView)v).getDrawable()).getBitmap();
    //Take the config of the bitmap. IT RETURNS NULL
    Bitmap.Config cfg= b.getConfig();
    //Copy the Bitmap and assign it to the new ImageView... IT CRASH (cfg == null)
    Bitmap b2 = b.copy(cfg, true);
    savedImage.setImageBitmap(b2);
    savingLayout.addView(savedImage);
}

那么为什么 b.getConfig() 返回 null?有解决方法吗?

谢谢

最佳答案

使用 Bitmap.Config.ARGB_8888 而不是 b.getConfig() 作为解决方法。

关于java - 为什么 Bitmap.getConfig() 返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9555810/

相关文章:

java - 布局既不能以编程方式工作,也不能以 xml 方式工作

java - PreparedStatement 如何避免或防止 SQL 注入(inject)?

android - 从多个 Activity 访问文件。

android - 在 Kotlin 中将 EditText(电话类型)转换为字符串

android - 如何防止收到广播消息

android - 如何修复 Android 模拟器上频繁出现的内存不足错误?

java - 哪个方面/拦截器处理@Transactional注释

java - Glide 错误 : java. lang.IllegalArgumentException - 您必须在后台线程上调用此方法

java - Android AVD 管理器 : Can't create new device definition (java. lang.NullPointerException)

java - 禁用 WebView 链接在模拟器上有效,但在设备上无效