public void onBindViewHolder(MyAdapter.MyViewHolde holder, int position) {
//how to set image for loading the images for gallery
----------
}
//以及如何从主要活动中的图库中获取图像的代码
答案 0 :(得分:1)
您必须首先从您的活动中获取图库中的所有图像,然后必须将图像列表设置为RecyclerView
使用以下方法获取所有图像 -
private ArrayList<String> getAllShownImagesPath(Activity activity) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}
现在将此图像列表设置为RecyclerView
适配器。
将此RecyclerView Example作为参考来设置所有图库图片。
答案 1 :(得分:1)
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolde> {
Context context;
private ArrayList<String> imageData = new ArrayList<String>();
public MyAdapter(ArrayList<String> imageData, FragmentActivity activity) {
this.imageData = imageData;
this.context = activity;
}
@Override
public MyAdapter.MyViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.inflate_single_image_row, parent, false);
return new MyViewHolde(itemView);
}
@Override
public void onBindViewHolder(MyAdapter.MyViewHolde holder, int position) {
String data = imageData.get(position);
if (data != null){
Glide.with(context).load(data).into(holder.singleImageView);
}else {
Toast.makeText(context, "Images Empty", Toast.LENGTH_SHORT).show();
}
}
@Override
public int getItemCount() {
return imageData.size();
}
public class MyViewHolde extends RecyclerView.ViewHolder {
ImageView singleImageView;
public MyViewHolde(View itemView) {
super(itemView);
singleImageView = (ImageView) itemView.findViewById(R.id.singleImageView);
}
}
}
答案 2 :(得分:1)
公共类GalleryFragment扩展了Fragment {
public GalleryFragment() {
// Required empty public constructor
}
private Context context;
private RecyclerView recyclerView;
ArrayList<String> imageData;
MyAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
context = getActivity();
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
recyclerView.setLayoutManager(layoutManager);
MyTask myTask = new MyTask();
myTask.execute();
return view;
}
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
imageData = getAllShownImagesPath(getActivity());
Log.e("imageData: ", String.valueOf(imageData.size()));
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
adapter = new MyAdapter(imageData, getActivity());
recyclerView.setAdapter(adapter);
}
}
private ArrayList<String> getAllShownImagesPath(Activity activity) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
cursor = activity.getContentResolver().query(uri, projection, null,
null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}
}
答案 3 :(得分:0)
这Get list of photo galleries on Android会帮助你.. 我想在这里添加的一件事是你应该在AsyncTask或Thread中添加库检索数据。在UI线程中获取图库数据不是一个好习惯,因为如果图库图像比你因为在UI处理中处理而得到ANR那么大。