自定义图库中选定图像的第一个图像大小

时间:2018-08-22 07:01:21

标签: java android size image-gallery

我正在开发一个自定义图片库。某些图片的大小为0Bytes,我的图片库不应允许图片的大小等于0。有没有可能找到图片大小的方法?还是避免加载此类图片。 这是我的代码

    Uri uri;
    Cursor cursor;
    int column_index_data;
    long sizeIndex;
    ArrayList<String> listOfAllImages = new ArrayList<>();
    String absolutePathOfImage;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, MediaStore.MediaColumns.DATE_ADDED + " DESC");

    if (cursor != null) {
        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);

        while (cursor.moveToNext()) {
            if (sizeIndex != 0) {
                Log.e("sizeimage", "" + sizeIndex);
                absolutePathOfImage = cursor.getString(column_index_data);
                listOfAllImages.add(absolutePathOfImage);
            }
        }
        thumbnailsselection = new boolean[cursor.getCount()];
        cursor.close();
    }

PS我不想在找到大小时将它们转换为位图。

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案,我们需要在元素上添加 MediaStore.MediaColumns.SIZE 作为投影数组。现在我可以从游标中获取值了。

    Uri uri;
    Cursor cursor;
    int column_index_data;
    long sizeIndex;
    ArrayList<String> listOfAllImages = new ArrayList<>();
    String absolutePathOfImage;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    // added size to my string array
    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.SIZE};

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, MediaStore.MediaColumns.DATE_ADDED + " DESC");

    if (cursor != null) {
        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        int imageSize = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);


        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);
            // I got the image size from my cursor.Now i can validate it whether it equals to 0 Or not
            long si = cursor.getLong(imageSize);
            if (si != 0)
                listOfAllImages.add(absolutePathOfImage);

        }
        thumbnailsselection = new boolean[cursor.getCount()];
        cursor.close();
    }