为什么bitmapfactory.decodefile返回null?
-
26-10-2019 - |
题
我正在尝试Android faceDetector。我需要使用一个bitmap文件(faces.bmp来自组照片),因为我没有找到一种在Android模拟器中使用Android摄像头的方法。但是bitmapfactory.decodefile返回null,并且该文档仅表示如果无法解码位图,则返回null。它只是一个24位.bmp文件。我在Windows 7上使用Eclipse。是否错误地指定了PathName?我需要使用24位.bmp文件以外的其他内容吗?
public class MyFaces extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final int width = 600;
final int height = 600;
final int maxFaces = 8;
FaceDetector faceDetector = new FaceDetector(width, height, maxFaces);
String pathName = "../res/drawable-hdpi/faces.bmp";
try {
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
Face faces[] = new Face[maxFaces];
int nFaces = faceDetector.findFaces(bitmap, faces);
Log.d(this.getClass().toString(), "Faces: " + nFaces);
} catch (Exception e) {
Log.e(this.getClass().toString(), e.getMessage(), e);
}
}
}
解决方案
如果您只是在测试,那么 Bitmap bitmap = BitmapFactory.decodeFile(pathName);
您可以使用:
Bitmap bitmap = BitmapFactory.decodeResource(R.drawable.faces);
不隶属于 StackOverflow