首页 > 解决方案 > 照片已删除,但在拍摄新照片时又出现

问题描述

我正在制作的应用程序需要拍照以将它们发送到服务器。

我至少需要拍6张照片。我有一个 recyclerView,我在其中显示我的照片的预览。它运行良好(我使用毕加索作为照片库)。

我需要能够在发送照片之前删除它们(以及因此它们的预览)。通过单击预览,我将其从我的照片选项卡中删除并使用 notifyDataSetChanged() 更新我的 recyclerview。照片消失。

当我拍摄另一张照片时,我有它的预览,但已删除照片的预览又回来了。如果我删除三张照片并拍摄一张新照片,我有新照片的预览和已删除的 3 张照片的预览。

这是我绑定视图的适配器的一部分

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        mImageView = holder.mPhotoIV;
        File mFile = new File(String.valueOf(Uri.parse(mListOfPhotos.get(position))));

        int width = mImageView.getLayoutParams().width;
        int height = mImageView.getLayoutParams().height;

        Picasso.get()
                .load(mFile)
                .resize(200, 200)
                .error(R.drawable.ic_no_photo_foreground)
                .into(mImageView, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

这是我调用适配器的活动的一部分

        mTakePhotoFAB.setOnClickListener(view -> {
            mDir = new File(getExternalCacheDir(), "PhotosAuthentifier");
            boolean success = true;

            if (!mDir.exists()) {
                success = mDir.mkdir();
            }

            if (success) {
                File mFile = new File(mDir, new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(new Date()) + ".jpg");

                mImageCapture.takePicture(mFile,
                        new ImageCapture.OnImageSavedListener() {
                            @Override
                            public void onImageSaved(@NonNull File file) {
                                mListOfPhotos.add(file.getAbsolutePath());
                                mNumberOfPhotoTV.setText(getResources().getString(R.string.minPhotos, mListOfPhotos.size()));

                                if (mListOfPhotos.size() >= 6) {
                                    mSendPhotoFAB.setVisibility(View.VISIBLE);
                                }

                                mAdapter = new CameraPhotoAdapter(mListOfPhotos, getBaseContext());
                                mRecyclerView.setAdapter(mAdapter);
                            }


                            @Override
                            public void onError(@NonNull ImageCapture.ImageCaptureError imageCaptureError, @NonNull String message, @Nullable Throwable cause) {
                                String mMessage = "Photo capture failed: " + message;
                                Toast.makeText(CameraActivity.this, mMessage, Toast.LENGTH_SHORT).show();

                                assert cause != null;
                                cause.printStackTrace();
                            }
                        });
            }

        });

删除图片的功能(在我的适配器中)

        protected void removePhoto(Context context, ArrayList<String> array, int position) {

            File mDir = new File(context.getExternalCacheDir(), "PhotosAuthentifier");
            File mFile = new File(array.get(position));

            if (mDir.exists()) {
                File[] mFilesIntoDir = mDir.listFiles();

                if (mFilesIntoDir == null) {
                    return;
                } else {
                    for (int i = 0; i < mFilesIntoDir.length; i++) {
                        if (mFilesIntoDir[i].getAbsolutePath().equals(mFile.getAbsolutePath())) {
                            boolean mSuccess = mFilesIntoDir[i].delete();

                            if (mSuccess) {
                                Picasso.get().invalidate(mFile.getAbsolutePath());
                                mListOfPhotos.remove(mFile.getAbsolutePath());
                                notifyDataSetChanged();
                            }

                        }
                    }
                }
            }
        }

我试图使毕加索缓存无效,但是当我拍摄新照片时,当我没有好的上传网址(黑色十字)时,我有默认行为,而不是重新出现已删除的照片

有人可以帮忙吗:)?

标签: androidandroid-recyclerviewpicassoandroid-camerax

解决方案


推荐阅读