如何检查“图库是否为空”

时间:2013-09-12 07:06:13

标签: android

我是Android新手,我需要知道如何检查设备图片库是否为空..

方法

File dir = new File(Environment.getExternalStorageDirectory()+ "/images");
File dird = new File("/mnt/sdcard");

    File[] files = dir.listFiles();
File[] filesd = dird.listFiles();

    if((files==null)||(filesd==null)){
    }

谢谢

2 个答案:

答案 0 :(得分:1)

试试这种方式

private boolean isImageGalleryEmpty() {

        try {
            final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media._ID;
            Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
            imagecursor.close();
            if (imagecursor.getCount() > 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
        }
        return true;

    }

答案 1 :(得分:1)

解决问题

    String[] projection = {MediaStore.Images.Media._ID};
     Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection, null,null,null);
    int columnIndex = ursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int size = cursor.getCount();
    // If size is 0, there are no images on the SD Card.
    if (size == 0) {

    } else {


    }

谢谢所有..