所以我有这个代码用于通过mediastore获取图像:
int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA );
mMediaStoreCursor.moveToPosition(position);
String dataString = mMediaStoreCursor.getString(dataIndex);
Uri mediaUri = Uri.parse("file://" + dataString);
return mediaUri;
此商品获取图片文件夹中的所有图片,我想更改它以获取特定文件夹中的所有图片,这些图片将作为字符串传递。例如{ android/picture/specificfolder/}
答案 0 :(得分:0)
选中此post
建议您使用光标并传递投影中所需的文件夹。
帖子的相关答案:
mediaCursor = getContentResolver().query( MediaStore.Files.getContentUri("external"),
null,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%YOUR_FOLDER_NAME%"},
null);
答案 1 :(得分:0)
您可以使用以下方法 -
public List<String> getFromSdcard()
{
ArrayList<String> imagePaths = new ArrayList<String>();// list of file paths
File[] listFile;
File file= new
File(android.os.Environment.getExternalStorageDirectory(),"android/picture/specificfolder/");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
imagePaths.add(listFile[i].getAbsolutePath());
}
}
return imagePaths;
}
使用
来检索图像List<String> sample = getFromSdcard();
for(int i=0; i<sample.size() ; i++){
final Uri image = Uri.parse("file://"+sample(i).toString());
}
答案 2 :(得分:0)
val DIRECTORY_NAME = "%your_folder_name%"
val selection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
MediaStore.MediaColumns.RELATIVE_PATH + " like ? "
else MediaStore.Images.Media.DATA + " like ? "
val selectionArgs = arrayOf(APP_RESOURCE_DIRECTORY_NAME)
val cursor = context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null,
selection,
selectionArgs,
null)