我使用 Firebase 存储和实时数据库分别存储图像及其下载 url。文件名以随机方式生成,下载 url 生成并保存到实时数据库中。
场景:如果用户上传新图像(例如个人资料图像),我想借助 downloadImageurl 删除旧图像(下载图像 url 在最初上传图像时生成,并保存在实时数据库中)。旧图像如何可以删除吗?我已经尝试过下面的代码,但为了让它工作,我必须获取文件名。
gcs
.bucket("e**********.appspot.com") // find it in Firebase>Storage>"gs://...." copy without gs
//or go to console.cloud.google.com/ buckets and copy name
.file("images/" +event.params.uid+"/"+filename) //file location in my storage
.delete()
.then(() => {
console.log(`gs://${bucketName}/${filename} deleted.`);
})
.catch(err => {
console.error('ERROR-DELETE:', err+ " filename: "+filename);
});
Run Code Online (Sandbox Code Playgroud) firebase firebase-realtime-database google-cloud-functions firebase-storage
我正在使用FragmentStatePagerAdapter在一个活动中显示大约5个片段。在每个活动中,我都显示了从FirebaseListAdapter / FirebaseRecyclerAdapter中获取的图像。由于是FragmentStagePagerAdapter,相邻视图/片段即使不可见也将被初始化。如果选项卡以快速方式滚动(前进/后退),应用程序崩溃并出现上述错误。我在互联网上发现了一些类似的问题,但没有能够解决此问题。如果我正在启动新活动(在活动中我正在网格视图中显示来自Firebase的图像)并立即将其关闭(请按返回按钮),您也无法启动被破坏活动错误的加载)我正在使用以下代码。
adapt= new FirebaseListAdapter<MyClassStudent>(getActivity(), MyClassStudent.class, R.layout.mychild_grid_template, myRef) {
@Override
protected void populateView(final View v, MyClassStudent model, int position) {
final TextView uname = (TextView) v.findViewById(R.id.mychild_uname);
final CircleImageView profileImage=(CircleImageView)v.findViewById(R.id.mychild_image);
studRef.child(model.getUsername()).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
Child child=dataSnapshot.getValue(Child.class);
uname.setText(child.getUsername());
Glide.with(getActivity()).load(child.getProfileImage()).into(profileImage);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
E/UncaughtException: java.lang.NullPointerException: You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually …Run Code Online (Sandbox Code Playgroud)