java - 为什么 BitmapFactory.decodeFile 返回 null?

标签 java android bitmapfactory

我正在试验 android FaceDetector。 我需要使用位图文件(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);

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

相关文章:

java - 如何将类中的对象存储在数组中

java - 语法错误;分配数组时

java - 将 T[](其中 T 是 Byte[])返回到 Byte[]

java - 缺少@AnnotationDrivenTx Spring3

java - 使用两个组件 Dagger2 时的 stackoverflow

android - 在可绘制图层列表 2 中调整位图大小

android - BitmapFactory.decodeFile 无法解析为类型

android - 如何调整从图库中获取的图像的大小?

android - 使用 "R.color.red"更改小部件中的文本颜色

Android BitmapFactory.decodeFile 变慢直到内存不足