Android:BitmapFactory.decodeResource 返回 null
我似乎无法弄清楚这一点。我有2个具有不同特征的java类,每个类都调用BitmapFactory.decodeResource来获取相同的图像资源,一个返回位图,另一个返回null。这两个类都在同一个包中。
这是有效的类,它调用 BitmapFactory.decodeResource 返回位图。我只包含了相关代码。
package advoworks.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainScreen extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainScreen.class.getSimpleName();
public MainScreen(Context context) {
super(context);
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
//adding the callback (this) to the surface holder to intercept events;
getHolder().addCallback(this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}
}
这是不起作用的类。 BitmapFactory.decodeResource 在调试中返回 NULL。我只包含了我认为相关的代码。
package advoworks.test;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
public class Segment {
private int x;
private int y;
private Bitmap bitmap;
public Segment(int x, int y) {
Log.d(TAG, "Creating Segment");
try {
this.bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
} catch (Exception e) {
Log.d(TAG,"Error is " + e);
}
this.x = x;
this.y = y;
Log.d(TAG, "Created Segment");
}
}
有人有任何线索吗?
I can't seem to figure this out. I have 2 java classes with different characteristics, each calling BitmapFactory.decodeResource to get the same image resource, one returns the bitmap while the other returns null. Both classes are in the same package.
Here is the class that works, it calls BitmapFactory.decodeResource which returns the bitmap. I've only included relevant code.
package advoworks.test;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainScreen extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = MainScreen.class.getSimpleName();
public MainScreen(Context context) {
super(context);
Bitmap bitmap;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
//adding the callback (this) to the surface holder to intercept events;
getHolder().addCallback(this);
// make the GamePanel focusable so it can handle events
setFocusable(true);
}
}
Here is the class that doesn't work. BitmapFactory.decodeResource returns a NULL in debug. I've only included code i felt was relevant.
package advoworks.test;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
public class Segment {
private int x;
private int y;
private Bitmap bitmap;
public Segment(int x, int y) {
Log.d(TAG, "Creating Segment");
try {
this.bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.droid_1);
} catch (Exception e) {
Log.d(TAG,"Error is " + e);
}
this.x = x;
this.y = y;
Log.d(TAG, "Created Segment");
}
}
Any clue anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查图像的分辨率,如果太大,BitmapFactory.decodeResource 将返回 null (也不例外)
Check the resolution of your image, if its too big, the BitmapFactory.decodeResource will just return null (no exception)
getResources()
是一个Context
类方法,并且您没有在 Segment 类中使用上下文。它是如何运作的。您应该调用getApplicationContext().getResources()
您应该将上下文传递给
Segment
构造函数。The
getResources()
is aContext
class method and you are not using a context in your Segment class. How does it work. You should callgetApplicationContext().getResources()
You should pass the context to the
Segment
constructor.确保您的图像不在 drawable-v24 文件夹中,将其移动到 drawable 文件夹中。
这对我有用。
make sure your image is not in drawable-v24 folder, move it to drawable folder.
this worked for me.
我的解决方案是创建一个可绘制对象(创建它没有任何问题),然后将其转换为位图
您还可以使用
drawable.width
和drawable.height
来获得相同的比例和分辨率My solution is to create a drawable (I had no issues creating it), then convert it to a bitmap
You can also use
drawable.width
anddrawable.height
to have the same scale and resolution