getImageCount()始终返回1

时间:2013-06-19 22:26:23

标签: android

我正在尝试检索设备在其图库中有多少图像的int值。我将在稍后使用它循环并抓取图像。

private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, CAMERA);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle extras = data.getExtras();
                bmp = (Bitmap) extras.get("data");
                MediaStore.Images.Media.insertImage(getContentResolver(), bmp,
                        "picture", "a picture");
                Log.v("HEY", "Your image should be in the gallery now");                
                Log.v("IMAGES", "Number of Images: " + getImageCount());
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.v("CANCELED", "The user has canceled the activity");
            }
        }

    }

    public int getImageCount() {
        File dir = new File(Environment.getExternalStorageDirectory() + "/Camera");
        File[] files = dir.listFiles();
        int numberOfImages = files.length;
        return numberOfImages;
    }

然而,logcat正在发布此

06-19 18:24:41.380: V/HEY(5367): Your image should be in the gallery now
06-19 18:24:41.380: V/IMAGES(5367): Number of Images: 1

所以它告诉我当图像的实际数量是14时有1张图像。我必须遗漏一些简单的东西,但我看不到它。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

知道了,来自@FoamyGuy的评论引发了一个想法。如果Camera在我的DCIM中,那么我需要更改此

File dir = new File(Environment.getExternalStorageDirectory() + "/Camera");

到这个

File dir = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");

这看起来很愚蠢,因为我以前从未指定过DCIM文件夹。它就是这样,我希望这有助于其他人。