試試這個:
創建一個鬥類
public class Bucket {
private String name;
private String firstImageContainedPath;
public Bucket(String name, String firstImageContainedPath) {
this.name = name;
this.firstImageContainedPath = firstImageContainedPath;
}
public String getName() {
return name;
}
public String getFirstImageContainedPath() {
return firstImageContainedPath;
}
}
然後,添加這種方法,它是將返回所有在有圖片的桶。
public static List<Bucket> getImageBuckets(Context mContext){
List<Bucket> buckets = new ArrayList<>();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String [] projection = {MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA};
Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
if(cursor != null){
File file;
while (cursor.moveToNext()){
String bucketPath = cursor.getString(cursor.getColumnIndex(projection[0]));
String fisrtImage = cursor.getString(cursor.getColumnIndex(projection[1]));
file = new File(fisrtImage);
if (file.exists() && !bucketSet.contains(bucketName)) {
buckets.add(new Bucket(bucketName, fisrtImage));
}
}
cursor.close();
}
return buckets;
}
最後,創建您的自定義微調項目並使用適配器填充微調項目。
下一步是使用選定存儲桶中的圖像填充gridview。 該方法將根據bucketpath返回所有圖像。
public List<String> getImagesByBucket(@NonNull String bucketPath){
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String [] projection = {MediaStore.Images.Media.DATA};
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME+" =?";
String orderBy = MediaStore.Images.Media.DATE_ADDED+" DESC";
List<String> images = new ArrayList<>();
Cursor cursor = mContext.getContentResolver().query(uri, projection, selection,new String[]{bucketPath}, orderBy);
if(cursor != null){
File file;
while (cursor.moveToNext()){
String path = cursor.getString(cursor.getColumnIndex(projection[0]));
file = new File(path);
if (file.exists() && !images.contains(path)) {
images.add(path);
}
}
cursor.close();
}
return images;
}
最後,創建你的適配器並填寫你的gridview。
我希望它能幫助你。
我想創建一個網格視圖和微調像insatgram,其中當我們選擇目錄時,它顯示所有圖像那裏使用此代碼 –
嗯,我明白了。首先,您應該獲取有圖像的存儲桶或文件夾列表以顯示微調器(第一張照片和存儲桶名稱)。然後,當你選擇一個文件夾時,gridview應該顯示文件夾中的所有圖像。等待我一段時間來改變答案。 – jmarkstar
感謝它現在加載圖像完美的網格視圖 –