Android - Get images for the Samsung S4 / S5 Gallery

I am working on my own image gallery for my application, but I had a serious problem in the Samsung Galaxy S4 and S5. The following code works fine on HTC and Xperia devices, but not on Samsung.

Basically, these devices always have null tags. Here is the code I made. On large lines, it returns a list of categories containing the name and list of images. The goal is to create a folder-based gallery, such as a native one.

If you have an idea, why doesnโ€™t it work in the Samsung Galaxy?

Thank you for your help.

 public List<Category> getCategories() { Map<String, Category> map = new HashMap<String, Category>(); String[] projection = new String[] { MediaStore.Images.Media._ID, MediaStore.Images.Media.BUCKET_DISPLAY_NAME, }; ContentResolver cr = getContentResolver(); Cursor cur = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media.DATE_TAKEN + " DESC"); if (cursor == null) return; if (cur.moveToFirst()) { Category category = null; long id = 0L; String bucket = null;; int idColumn = cur.getColumnIndex(MediaStore.Images.Media._ID); int bucketColumn = cur.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); do { id = cur.getLong(idColumn); bucket = cur.getString(bucketColumn); if (map.get(bucket) == null) { category = new Category(bucket); map.put(bucket, category); } category = map.get(bucket); category.addImage( idToImage(id) ); } while (cur.moveToNext()); } return map.values().toArray(); } private Image idToImage(long id) { Image image = new Image(); image.setThumbnail( getThumbnail(id) ); image.setImage( getImage(id) ); return image; } private String getThumbnail(long id) { String path = null; Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(getContentResolver(), id, MediaStore.Images.Thumbnails.MINI_KIND, null); if( cursor != null && cursor.getCount() > 0 ) { cursor.moveToFirst(); path = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) ); cursor.close(); } return path; } private String getImage(long id) { String path = null; String[] projection = {MediaStore.Images.Media.DATA}; String where = MediaStore.Images.Media._ID + " = " + id; Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, where, null, null); int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA); if( cursor != null && cursor.getCount() > 0 ) { cursor.moveToFirst(); path = cursor.getString(dataColumn); } return path; } 
+6
source share
1 answer

He will work

 /** * Get actual file path from gallery */ public String getPath(Uri uri, Context ctx) { String[] projection = {MediaStore.Images.Media.DATA}; String result; Cursor cursor = ctx.getContentResolver().query(uri, projection, null, null, null); if (cursor == null) { return null; } int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); cursor.close(); return result; } 
0
source

Source: https://habr.com/ru/post/971527/


All Articles