【发布时间】:2021-06-12 07:39:06
【问题描述】:
我正在使用MediaStore 获取absoluteImagePaths 并将其提供给适配器类中的Glide。
目前我正在使用MediaStore.MediaColumns.DATA 来获取路径。但是,最近 google 弃用了该 api,并且在不久的将来可能无法使用。那么,这个的替代品是什么?
...
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.MediaColumns.DATA,
MediaStore.Images.Media._ID
};
String orderBy = MediaStore.Images.Media.DEFAULT_SORT_ORDER;
cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
while (cursor.moveToNext()) {
String absoluteImagePath = cursor.getString(column_index_data);
ImageModel ImageModel = new ImageModel();
ImageModel.setPath(absoluteImagePath);
arrayList.add(ImageModel);
}
...
这段代码给出了绝对路径。然后它被喂食以滑过arrayList。
已尝试的解决方案
- 我尝试使用
MediaStore.Images.Media._ID,但它不起作用。 - 我尝试点击此链接MediaStore.MediaColumns.DATA is deprecated, and I want to load images from gallery to my app,但是我找不到适合我的特定代码的替代品。
此链接建议使用
MediaStoroe.Images.Media._ID并稍作调整。但是,这个调整给了一个 Uri 而不是绝对路径。而且我的代码需要生成absoluteImagePath以提供给Glide。 - 然后我尝试通过此链接Get filename and path from URI from mediastore 从
Uri获取path,但是,它又导致我再次访问MediaStore.MediaColumns.DATA。 - 我也尝试了 google,但似乎我也需要更改其他一些类才能使用
MediaStore.Images.Media._ID。
注意:
- 我有
ImageModel类到get和setpath的图像。 - 我有
Adapter类,其方法为onBindViewHolder,其中包含Glide方法
...
Glide.with(context)
.load("file://" + arrayList.get(position).getPath())
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(holder.img);
...
【问题讨论】:
-
But, this tweaks gives an Uri是的。使用 uri。从模型中删除 .setPath() 并添加 .setUri(uri)。然后像 Saurabh 向您展示的那样从 uri 加载。 -
我做到了。它奏效了。谢谢。但是,现在我尝试了一些新的东西并遇到了一些问题。 stackoverflow.com/questions/67949336/… 请访问此链接。
标签: android uri media android-glide mediastore