gpt4 book ai didi

android - 仅从画廊获取图像

转载 作者:行者123 更新时间:2023-11-30 00:34:31 25 4
gpt4 key购买 nike

我需要获取相机(图库)拍摄的所有图像的 uri/路径。我如何修改下面的代码以仅提供图库中的图像。下面的代码也为我提供了其他文件夹中的图像。

public ArrayList<String> getImages()
{
ArrayList<String> paths = new ArrayList<String>();
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media.DATE_ADDED;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();

//Create an array to store path to all the images
String[] arrPath = new String[count];

for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
paths.add(arrPath[i]);

}

return paths;
}

最佳答案

只需在查询中提供选择参数

public ArrayList<String> getImages()
{
ArrayList<String> paths = new ArrayList<String>();
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
String[] selectionArgs = new String[] {
"Camera"
};
final String orderBy = MediaStore.Images.Media.DATE_ADDED;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, orderBy);
//Total number of images
int count = cursor.getCount();

//Create an array to store path to all the images
String[] arrPath = new String[count];

for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
paths.add(arrPath[i]);

}

return paths;
}

关于android - 仅从画廊获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43648036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com