gpt4 book ai didi

java - 从SD卡获取图像并在Gridview中显示

转载 作者:行者123 更新时间:2023-12-02 07:57:07 26 4
gpt4 key购买 nike

我正在尝试从 SD 卡获取所有图像并将它们显示在 GridView 中(包含在 fragment 中)。然而,虽然没有抛出异常,但 gridview 中没有显示任何内容,只是纯黑屏。我不确定问题出在哪里,是 View 的绑定(bind)还是将数据提取到游标中。这是该 fragment 的当前代码:

public class PhotoGridFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

// member variables for
private static final int PHOTO_LIST_LOADER = 0x01;
private ImageCursorAdapter adapter;
private Cursor c;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLoaderManager().initLoader(PHOTO_LIST_LOADER, null, this);

adapter = new ImageCursorAdapter(getActivity().getApplicationContext(), c);
}

/* R.layout.grid_item,
null, new String[] { MediaStore.Images.Thumbnails.DATA }, new int[] {R.id.grid_item},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); */

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.photo_item, container, false);
}


// Loader manager methods
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA };
CursorLoader cursorLoader = new CursorLoader(getActivity(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
null, null, null);
return cursorLoader;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);

}

public void onLoaderReset(Loader<Cursor> cursor) {
adapter.swapCursor(null);
}

private class ImageCursorAdapter extends CursorAdapter {

private LayoutInflater mLayoutInflater;
private Context mContext;

public ImageCursorAdapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView newView = (ImageView) view.findViewById(R.layout.grid_item);
String imagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
if (imagePath != null && imagePath.length() != 0 && newView != null) {
newView.setVisibility(ImageView.VISIBLE);
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
return v;
}


}

该项目的布局文件如下:

photo_item.xml:

<?xml version="1.0" encoding="UTF-8"?>
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/photo_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp"

/>

最佳答案

在bindView中,您实际上并没有将imageView的drawable设置为任何内容。您获取图像路径,验证它是真实路径,然后忽略它:) 使用该路径获取可绘制对象!然后将 imageView 的可绘制对象设置为该图像。

关于java - 从SD卡获取图像并在Gridview中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9472911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com