从 firebase 检索图像时出错

Posted

技术标签:

【中文标题】从 firebase 检索图像时出错【英文标题】:error for retrieving an images from firebase 【发布时间】:2020-06-14 11:14:06 【问题描述】:

我在从应用程序中删除图像时遇到此错误

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

这是我的代码,我写它以供删除

Images_Actvity.java:

public class Images_Avctivity extends AppCompatActivity implements Image_adapter.OnItemClickListener 
    private RecyclerView mRecyclerView;
    private 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:

 public class Image_adapter extends RecyclerView.Adapter<Image_adapter.ImageViewHolder> 

        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;
        
    

【问题讨论】:

使用这个 getStorage() 作为参考FromUrl FirebaseStorage.getInstance().getStorage().getReferenceFromUrl(fileURL); 请提供数据库结构 请将您的数据库结构添加为 JSON 文件或至少是屏幕截图。也请回复@AlexMamo 我已经添加了一个数据结构......我可以上传我的图片......问题是在将数据检索到我的应用程序时 【参考方案1】:
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");
     
); 

【讨论】:

以上是关于从 firebase 检索图像时出错的主要内容,如果未能解决你的问题,请参考以下文章

在客户端检索由 firebase 分配的用户 ID 令牌时出错

使用 KOTLIN 从 firebase 检索数据时出错

从 Firebase 检索第二个项目时出错

将数据从 FireBase 检索到颤振项目时出错

将图片从 URL 上传到 Firebase 存储

从firebase异步加载图像时出错