问题描述
我正在尝试解决这个问题。 我有一些代码使用CursorLoader从sdcard查询图像,并在SimpleCursorAdapter的帮助下将它们显示在GridView中。 我在网上跟踪了一些示例,并通读了文档。 我知道我做得对。 该代码运行没有任何错误,但是我无法使缩略图显示在网格上。 以下是到目前为止我得到的代码:
GalleryFragment.java
public class GalleryFragment extends Fragment {
private SimpleCursorAdapter adapter;
public static final int PHOTO_LIST_ID = 0x01;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
// Bind adapter to grid
setupCursorAdapter();
// Initialize the loader with a special ID and the defined callbacks
getLoaderManager().initLoader(PHOTO_LIST_ID,
new Bundle(), imageLoader);
GridView gridView = (GridView) view.findViewById(R.id.gridView);
gridView.setAdapter(adapter);
return view;
}
private void setupCursorAdapter() {
String[] from = {MediaStore.Images.Thumbnails.DATA};
int[] to = {R.id.imageView};
adapter = new SimpleCursorAdapter(getActivity().getBaseContext(),
R.layout.gallery_image_item,
null, from, to,
0);
}
private LoaderManager.LoaderCallbacks<Cursor> imageLoader =
new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Define the columns to retrieve
String[] projection = {MediaStore.Images.Thumbnails._ID,
MediaStore.Images.Thumbnails.DATA};
return new CursorLoader(getActivity(),
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
};
}
fragment_gallery.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.sfsu.csc780.pictachio.GalleryFragment">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gridView"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"/>
</LinearLayout>
gallery_image_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:padding="10dp"
android:adjustViewBounds="true"
android:contentDescription="ImageView" />
</FrameLayout>
我在很多地方都读到,RecyclerView是推荐的方法,而不是GridView。 我已经对此进行了尝试,但是我不知道要为此创建哪种模型。 任何帮助,不胜感激。 谢谢。
1楼
我不了解使用getLoaderManager()和initLoader()获取图像。 一种方法是从SQLite数据库中获取它们。 正确的Google文档是@SQL 。 我提供了使用该查询方法的示例代码。 但是,我从来不需要在Android中获取图像。
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] cols = new String[]{
MediaStore.Images.Media.DATA, // get the image
};
//String where = MediaStore.Images.Media.DATA;
Cursor cursor = getContentResolver().query(uri, cols, null, null, null);
if(cursor.moveToFirst()) {
int column_image = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_image);
}
cursor.close();
笔记:
-
query
()的第三个和第四个参数对于过滤返回的数据很有用。 -
cursor
对象是从查询返回的,并且可以遍历,上面列出了示例代码。 - 另一个有趣的方法是使用@ 类环境 。 我用它来查找特定目录中的文件 。 该技术实际上可能更快,因为它在特定目录中搜??索而不是查询数据库。 但是,可能存在意外的权限问题,尤其是SD卡。
试试看,让我们知道会发生什么。
2楼
最好的方法是使用库。 很简单。 只需将库依赖项添加到您的项目中,然后将以下行添加到您的类中。 您的适配器已准备就绪,可以将其设置为gridview。
RecentImages ri = new RecentImages();
ImageAdapter adapter = ri.getAdapter(MainActivity.this);
它具有一些用于自定义图像查询的选项。