我正在尝试从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.
更多推荐
发布评论