从Firebase检索图像时出错

问题描述 投票:1回答:1

从应用程序删除图像时出现此错误

E / InputEventReceiver:调度输入事件的异常。E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallbackE / MessageQueue-JNI:java.lang.IllegalArgumentException:无法解析存储Uri。

这是我写的我要删除的代码

Images_Actvity.java:

公共类Images_Avctivity扩展了AppCompatActivity,它实现了Image_adapter.OnItemClickListener {私人RecyclerView mRecyclerView;私人Image_adapter mAdapter;

private ProgressBar mProgressCircle;

private FirebaseStorage mStorage;
private DatabaseReference mDatabaseRef;
private ValueEventListener mDBListener;

private List<Upload> mUploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_images__avctivity);

    mRecyclerView = findViewById(R.id.recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    mAdapter = new Image_adapter(Images_Avctivity.this, mUploads);

    mRecyclerView.setAdapter(mAdapter);

    mAdapter.setOnItemClickListener(Images_Avctivity.this);

    mStorage = FirebaseStorage.getInstance();
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    mDBListener = mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            mUploads.clear();

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                upload.setKey(postSnapshot.getKey());
                mUploads.add(upload);
            }

            mAdapter.notifyDataSetChanged();

            mProgressCircle.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(Images_Avctivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            mProgressCircle.setVisibility(View.INVISIBLE);
        }
    });
}

@Override
public void onItemClick(int position) {
    Toast.makeText(this, "Normal click at position: " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onWhatEverClick(int position) {
    Toast.makeText(this, "Whatever click at position: " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onDeleteClick(int position) {
    Upload selectedItem = mUploads.get(position);
    final String selectedKey = selectedItem.getKey();

    StorageReference imageRef = mStorage.getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            mDatabaseRef.child(selectedKey).removeValue();
            Toast.makeText(Images_Avctivity.this, "Item deleted", Toast.LENGTH_SHORT).show();
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mDatabaseRef.removeEventListener(mDBListener);
}

}

这是适配器代码:

Image_adapter.java:

公共类Image_adapter扩展了RecyclerView.Adapter {

private Context mContext;
private List<Upload> mUploads;
private OnItemClickListener mListener;

public Image_adapter(Context context, List<Upload> uploads) {
    mContext = context;
    mUploads = uploads;
}

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

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.with(mContext)
            .load(uploadCurrent.getImageUrl())
            .placeholder(R.mipmap.ic_launcher)
            .fit()
            .centerCrop()
            .into(holder.imageView);
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
        View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
    public TextView textViewName;
    public ImageView imageView;

    public ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {
                mListener.onItemClick(position);
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select Action");
        MenuItem doWhatever = menu.add(Menu.NONE, 1, 1, "Do whatever");
        MenuItem delete = menu.add(Menu.NONE, 2, 2, "Delete");

        doWhatever.setOnMenuItemClickListener(this);
        delete.setOnMenuItemClickListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position != RecyclerView.NO_POSITION) {

                switch (item.getItemId()) {
                    case 1:
                        mListener.onWhatEverClick(position);
                        return true;
                    case 2:
                        mListener.onDeleteClick(position);
                        return true;
                }
            }
        }
        return false;
    }
}

public interface OnItemClickListener {
    void onItemClick(int position);

    void onWhatEverClick(int position);

    void onDeleteClick(int position);
}

public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}

}

java android database firebase-realtime-database firebase-storage
1个回答
0
投票
StorageReference storageReference = FirebaseStorage.getInstance().getReferenceFromUrl("https://firebasestorage.googleapis.com/v0/b/***********************-5fac-45b6-bbda-ed4e8a3a62ab");
storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() { 
@Override 
public void onSuccess(Void aVoid) {
    // File deleted successfully 
    Log.e("firebasestorage", "onSuccess: deleted file");
    } 
}).addOnFailureListener(new OnFailureListener() { 
@Override 
public void onFailure(@NonNull Exception exception) {
    // Uh-oh, an error occurred! 
    Log.e("firebasestorage", "onFailure: did not delete file");
    } 
}); 
© www.soinside.com 2019 - 2024. All rights reserved.