android - 从具有内部存储的Android手机上的图库中获取图像

标签 android gridview image-gallery android-gallery

您好,我正在开发一个 Android Gallery 应用程序,我从内置的图库中获取图像并显示它。我正在使用如下代码

        String[] projection = {MediaStore.Images.Thumbnails._ID};


   Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null);


        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int size = cursor.getCount();
        // If size is 0, there are no images on the SD Card.
        if (size == 0) 
        {
            Log.e("size 0","0");
        }

问题是当我在只有内部存储的手机 (Galaxy Nexus) 上运行此代码时,我得到的日志显示大小为零,即使内置图库中有图像。我该如何解决这个问题。 请帮助。谢谢!

最佳答案

要获取画廊图像列表,您可以尝试这样做

String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN
};


Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


Cursor cur = getContentResolver().query(imageUri,
        projection, // Which columns to return
        null,       // Which rows to return (all rows)
        null,       // Selection arguments (none)
        null        // Ordering
        );

Log.i("Images Count"," count="+cur.getCount());

关于android - 从具有内部存储的Android手机上的图库中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18225178/

相关文章:

android - TeamCity 与 Android Lint 报告的集成

android - 如何在运行时android找出自定义对话框的宽度和高度

ios - 在uigridview中的行和列设置图像

html - 无法将按钮保持在图像底部 - 在响应式图像库中

asp.net - Javascript或CSS悬停在Safari和Chrome中不起作用

php - DirectoryIterator() 以某种方式复制路径

android - React-Native : react-native-fast-image not working properly with react-native, 它给出错误

java - 将 XMPP 协议(protocol)合并到 Android 应用程序中

java - GridView 内的按钮未获得正确的 ID

gridview - yii2 gridview 范围过滤器