如何使用New Folder Picker API将图像从SD卡显示到ListView?(How to Display images from SD Card to ListView with the

系统教程 行业动态 更新时间:2024-06-14 16:57:40
如何使用New Folder Picker API将图像从SD卡显示到ListView?(How to Display images from SD Card to ListView with the New Folder Picker API?)

我正在尝试从SD卡中选择一个文件夹并显示该文件夹中的图像列表。

它在Android 5.0之前工作正常,所以我开始使用New SD Card Access API更新它以选择文件夹。

public void FolderSelectionAlertDialog_Lollipop() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 556); } @Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if(resultCode == RESULT_OK) { // Get Uri from Storage Access Framework. Uri_Lollipop = resultData.getData(); // Persist access permissions. this.getContentResolver().takePersistableUriPermission(Uri_Lollipop, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } }

由于某些原因,ListView中没有显示图像。

这是代码:

public void ListView_Load_Lollipop() { DocumentFile documentFile = DocumentFile.fromTreeUri(this, Uri_Lollipop); Locale defaultLocale = Locale.getDefault(); for(DocumentFile file : documentFile.listFiles()) { String FileName = file.getName(); if(FileName.toLowerCase(defaultLocale).endsWith(".jpg") || FileName.toLowerCase(defaultLocale).endsWith(".png") || FileName.toLowerCase(defaultLocale).endsWith(".jpeg")) { mListViewObject = new ListViewObject(); mListViewObject.setName(FileName); Uri uri = file.getUri(); String imagePath = uri.getPath(); mListViewObject.setImageUrl("file:///" + imagePath); ListViewObject_List.add(mListViewObject); } } }

适配器:

public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = inflater.inflate(R.layout.listview_layout, parent, false); viewHolder.imageView = (ImageView)convertView.findViewById(R.id.ListViewImage); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder)convertView.getTag(); } Picasso.with(mContext) .load(getItem(position).getImageUrl()) .fit() .centerCrop() .into(viewHolder.imageView); } class ViewHolder { RelativeLayout layout; ImageView imageView; }

有人可以指导我吗?

非常感谢

更新:4-28-16

我忘了添加这个

listViewAdapter = new ListViewAdapter(this, ListViewObject_List); listView.setAdapter(listViewAdapter);

public void ListView_Load_Lollipop() { DocumentFile documentFile = DocumentFile.fromTreeUri(this, Uri_Lollipop); Locale defaultLocale = Locale.getDefault(); for(DocumentFile file : documentFile.listFiles()) { String FileName = file.getName(); if(FileName.toLowerCase(defaultLocale).endsWith(".jpg") || FileName.toLowerCase(defaultLocale).endsWith(".png") || FileName.toLowerCase(defaultLocale).endsWith(".jpeg")) { mListViewObject = new ListViewObject(); mListViewObject.setName(FileName); Uri uri = file.getUri(); String imagePath = uri.getPath(); mListViewObject.setImageUrl("file:///" + imagePath); ListViewObject_List.add(mListViewObject); } } }

感谢@ CommonsWare的回答:我能够通过将图像Uri而不是图像路径直接传递给对象类来解决我的问题。

我希望将来可以帮助别人。

I am trying to select a folder from SD Card and display the list of images inside this folder.

It was working fine until Android 5.0, so I started updating it with the New SD Card Access API to pick the folder.

public void FolderSelectionAlertDialog_Lollipop() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 556); } @Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { if(resultCode == RESULT_OK) { // Get Uri from Storage Access Framework. Uri_Lollipop = resultData.getData(); // Persist access permissions. this.getContentResolver().takePersistableUriPermission(Uri_Lollipop, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } }

For some reasons, there is no image showing in the ListView.

Here is the code:

public void ListView_Load_Lollipop() { DocumentFile documentFile = DocumentFile.fromTreeUri(this, Uri_Lollipop); Locale defaultLocale = Locale.getDefault(); for(DocumentFile file : documentFile.listFiles()) { String FileName = file.getName(); if(FileName.toLowerCase(defaultLocale).endsWith(".jpg") || FileName.toLowerCase(defaultLocale).endsWith(".png") || FileName.toLowerCase(defaultLocale).endsWith(".jpeg")) { mListViewObject = new ListViewObject(); mListViewObject.setName(FileName); Uri uri = file.getUri(); String imagePath = uri.getPath(); mListViewObject.setImageUrl("file:///" + imagePath); ListViewObject_List.add(mListViewObject); } } }

Adapter:

public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = inflater.inflate(R.layout.listview_layout, parent, false); viewHolder.imageView = (ImageView)convertView.findViewById(R.id.ListViewImage); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder)convertView.getTag(); } Picasso.with(mContext) .load(getItem(position).getImageUrl()) .fit() .centerCrop() .into(viewHolder.imageView); } class ViewHolder { RelativeLayout layout; ImageView imageView; }

Can someone please guide me on this?

Thanks a lot

Update: 4-28-16

I forgot to add this

listViewAdapter = new ListViewAdapter(this, ListViewObject_List); listView.setAdapter(listViewAdapter);

to

public void ListView_Load_Lollipop() { DocumentFile documentFile = DocumentFile.fromTreeUri(this, Uri_Lollipop); Locale defaultLocale = Locale.getDefault(); for(DocumentFile file : documentFile.listFiles()) { String FileName = file.getName(); if(FileName.toLowerCase(defaultLocale).endsWith(".jpg") || FileName.toLowerCase(defaultLocale).endsWith(".png") || FileName.toLowerCase(defaultLocale).endsWith(".jpeg")) { mListViewObject = new ListViewObject(); mListViewObject.setName(FileName); Uri uri = file.getUri(); String imagePath = uri.getPath(); mListViewObject.setImageUrl("file:///" + imagePath); ListViewObject_List.add(mListViewObject); } } }

Thanks to @CommonsWare's answer: I was able to resolve my problem by passing the image Uri instead of the image path directly to the object class.

I hope it helps someone else in the future.

最满意答案

不要求Uri以文件扩展名结尾。 在DocumentFile上使用getType()并查看MIME类型是否为image类型。

"file:///" + imagePath完全没有意义。 Uri不是文件。 将Uri本身传递给任何setImageUrl() 。

There is no requirement that a Uri end in a file extension. Use getType() on your DocumentFile and see if the MIME type is of type image.

"file:///" + imagePath is utterly meaningless. A Uri is not a file. Pass the Uri itself into whatever setImageUrl() is.

更多推荐

本文发布于:2023-04-13 12:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/922329537a5f210c1142170e3bcee3ea.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   图像   Picker   API   Card

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!