0

我在 RecyclerView 中创建交错视图并显示 SD 卡文件夹图像。

我所做的:我设法在 SD 卡上创建文件夹并将所有点击的图像保存在那里。

问题:现在我无法在交错回收站视图的文件夹中显示这些图像。

回收站视图布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout_signup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">


        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view_cameragallery"
            android:layout_marginTop="10dp"
            android:layout_below="@+id/view3"/>

        </RelativeLayout>

    </android.support.v7.widget.CardView>

    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

RecyclerActivity.class

 StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        cameraRecyclerView.setLayoutManager(gridLayoutManager);
         adapter = new CameraAdapter(new ArrayList<String>(),getContext());
        cameraRecyclerView.setAdapter(adapter);

相机按钮点击手柄(将图像保存在 SD 卡文件夹中):

 @Override
    public void onClick(View v) {

        newpath = new File(Environment.getExternalStorageDirectory(),
                photoPath + "_" + holder.phone + "/");

        // Create the storage directory if it does not exist
        newpath.mkdirs();

        checkinUUID = UUID.randomUUID();


        //media file name
         tempphoto = new File(newpath,checkinUUID + ".jpg");

        /*create file to save image*/
        photoUri = Uri.fromFile(tempphoto);

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        //set the image file name
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);

        // handle the returned data in onActivityResult
        startActivityForResult(intent,
                Constant.TAKE_CAMERA_PICTURE_REQUEST);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {

            if (requestCode == Constant.TAKE_CAMERA_PICTURE_REQUEST) {

                if (newpath.isDirectory() == false) {
                    newpath.mkdirs();
                }

                if (newpath.isDirectory() == true) {

                    if(newpath.exists())
                    paths = new String[]{tempphoto.getPath()};
                    MediaScannerConnection.scanFile(getActivity(), paths, null, null);
                    listofImagesPath =new ArrayList<String>();
                    listofImagesPath = RetriveCapturedImagePath();
                    if(listofImagesPath!=null){
                        adapter.addApplications(listofImagesPath);

                    }

                }
            }
        }

        super.onActivityResult(requestCode, resultCode, data);

    }

RetriveCapturedImagePath()

 private ArrayList<String> RetriveCapturedImagePath() {
        ArrayList<String> tFileList = new ArrayList<String>();

        if (newpath.exists()) {
            File[] files=newpath.listFiles();
            Arrays.sort(files);

            for(int i=0; i<files.length; i++){
                File file = files[i];
                if(file.isDirectory())
                    continue;
                tFileList.add(file.getPath());
            }
        }
        return tFileList;
    }

适配器类:

public class CameraAdapter extends RecyclerView.Adapter<CameraAdapter.ViewHolder> {

    private ArrayList<String> imagesPath;
    private Context context;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item_camera_view, parent, false);
        return new ViewHolder(v);
    }

    public CameraAdapter(ArrayList<String> imagesPath, Context context) {
        this.imagesPath = imagesPath;
        this.context = context;

    }

    public void addApplications(ArrayList<String> candidates) {
        this.imagesPath.addAll(candidates);
        this.notifyItemRangeInserted(0, candidates.size() - 1);

    }

    public void clearApplications() {
        int size = this.imagesPath.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                imagesPath.remove(0);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        for (int i=0;i<imagesPath.size();i++){
            holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position)));
        }
//        Bitmap bm = BitmapFactory.decodeFile(imagesPath)
    }


    @Override
    public int getItemCount() {
        return imagesPath.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageview ;
        public TextView imageText;
        public ViewHolder(View itemView) {
            super(itemView);
           this.imageview = (ImageView)itemView.findViewById(R.id.camera_image_view);
//            this.imageText = (TextView) itemView.findViewById(R.id.camera_grid_text_id);


        }
    }
}

single_item_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/camera_image_view"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="hello"/>



</LinearLayout>

我在上面的代码中有 SD 卡文件夹中的所有图像。但无法在交错视图中显示它们。

请帮忙

4

1 回答 1

0

问题似乎出在您RecyclerActivity.class的. 这就是为什么你没有显示任何东西。AdapterRecyclerViewArrayListRecyclerView

    adapter = new CameraAdapter(new ArrayList<String>(),getContext());
    cameraRecyclerView.setAdapter(adapter);

您应该ArrayList像这样设置 imagePaths :

    adapter = new CameraAdapter(imagePaths, getContext());
    cameraRecyclerView.setAdapter(adapter);

然后当数据发生变化时(在用检索到ArrayList的数据填充之后),只需调用's . 这应该够了吧。notifyDatasetChanged()RecyclerViewAdapter

此外,在您的onBindViewHolder方法中,您有一个不必要的 for 循环:

     for (int i=0;i<imagesPath.size();i++){
         holder.imageview.setImageBitmap(BitmapFactory.decodeFile(imagesPath.get(position)));
     }
于 2016-05-12T09:13:24.383 回答