数据未存储到 Firebase 实时数据库 - Android Studio
Data is not storing to Firebase Realtime Databse - Android Studio
//请帮忙:
我想做的事情:
我想从应用程序上传视频到 firebase 并创建实时数据库 child 来存储上传的视频 link 和更多信息。
出现什么错误:
视频已成功上传,但数据库 child 未在实时数据库中创建。
请帮我解决问题。
这是我的代码:-
AddReel.java
public class AddReel extends AppCompatActivity {
ImageView cancel,pick_image;
Button reel;
Button browse,upload;
MediaController mediaController;
EditText vtitle;
VideoView videoView;
EditText description;
CardView reel_card;
ProgressDialog progressDialog;
// profileUrl,username,memer
String reel_url;
Uri videouri;
FirebaseUser user;
FirebaseAuth auth;
DatabaseReference userRef,reelRef;
DatabaseReference databaseReference;
StorageReference storageReference;
private long counterReel=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reel);
init();
auth=FirebaseAuth.getInstance();
user=auth.getCurrentUser();
vtitle=(EditText)findViewById(R.id.vtitle);
storageReference= FirebaseStorage.getInstance().getReference().child("Reels");
userRef= FirebaseDatabase.getInstance().getReference().child("Users");
reelRef=FirebaseDatabase.getInstance().getReference("Reels");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(AddReel.this,new String[]
{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
// getData();
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent,1);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadReel();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==RESULT_OK)
{
if (data !=null)
{
videouri=data.getData();
videoView.setVideoURI(videouri);
}
}else
{
Toast.makeText(this, "Something went wrong!!", Toast.LENGTH_SHORT).show();
}
}
private void uploadReel()
{
progressDialog=new ProgressDialog(AddReel.this);
progressDialog.setTitle("New Post");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
if (videouri == null)
{
Toast.makeText(this, "No Meme select!!", Toast.LENGTH_SHORT).show();
}else
{
final StorageReference sRef=storageReference.child(System.currentTimeMillis()+"."+getExtension(videouri));
sRef.putFile(videouri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
sRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
reel_url=uri.toString();
//database
// SharedPreferences.Editor editor=getSharedPreferences("post",MODE_PRIVATE).edit();
// editor.putString("id",postid);
// editor.apply();
saveReelsDataInFirebase(reel_url);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(AddReel.this, "Error"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
double progress=(100*snapshot.getBytesTransferred()/snapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded "+((int)progress) +"%...");
}
});
}
}
private void init()
{
videoView=(VideoView)findViewById(R.id.videoView);
upload=(Button)findViewById(R.id.upload);
browse=(Button)findViewById(R.id.browse);
mediaController=new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
}
public String getExtension(Uri videouri)
{
MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(getContentResolver().getType(this.videouri));
}
private void saveReelsDataInFirebase(final String postReel)
{
reelRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists())
{
counterReel=snapshot.getChildrenCount();
}else
{
counterReel=0;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
userRef.child(user.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists())
{
String n=snapshot.child("username").getValue().toString();
String m=snapshot.child("memer").getValue().toString();
String p=snapshot.child("profileUrl").getValue().toString();
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("dd-M-yyyy hh:mm a");
String currentDate=format.format(date);
String reelid=reelRef.push().getKey();
HashMap<String,Object> map=new HashMap<>();
map.put("date",currentDate);
map.put("reelid",reelid);
map.put("title",vtitle);
map.put("postReel",postReel);
map.put("publisher",user.getUid());
map.put("profile",p);
map.put("memer",m);
map.put("username",n);
map.put("counterPost",counterReel);
progressDialog.dismiss();
Toast.makeText(AddReel.this, "New post added!!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddReel.this,HomeActivity.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
型号class:-
Filemodel.java
public class Filemodel {
private String date;
private String reelid;
private String title;
private String postReel;
private String publisher;
private String profile;
private String memer;
private String username;
private long counterReel;
public Filemodel(String date, String reelid, String title, String postReel, String publisher, String profile, String memer, String username, long counterReels) {
this.date = date;
this.reelid = reelid;
this.title = title;
this.postReel = postReel;
this.publisher = publisher;
this.profile = profile;
this.memer = memer;
this.username = username;
this.counterReel = counterReels;
}
public Filemodel()
{
}
public String getDate() {
return date;
}
public String getreelid() {
return reelid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getpostReel() {
return postReel;
}
public void setpostreel(String postreel) {
this.postReel = postreel;
}
public String getPublisher() {
return publisher;
}
public String getProfile() {
return profile;
}
public String getMemer() {
return memer;
}
public String getUsername() {
return username;
}
public long getCounterPost() {
return counterReel;
}
}
第一步应该是停止忽略错误并实施 onCancelled
。至少应该是:
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
除此之外,我在您共享的代码中没有看到任何对 setValue
或 updateChildren
的调用,它们是唯一将数据写入数据库的调用。所以你可能想看看 writing data.
上的文档
//请帮忙:
我想做的事情:
我想从应用程序上传视频到 firebase 并创建实时数据库 child 来存储上传的视频 link 和更多信息。
出现什么错误: 视频已成功上传,但数据库 child 未在实时数据库中创建。
请帮我解决问题。
这是我的代码:- AddReel.java
public class AddReel extends AppCompatActivity {
ImageView cancel,pick_image;
Button reel;
Button browse,upload;
MediaController mediaController;
EditText vtitle;
VideoView videoView;
EditText description;
CardView reel_card;
ProgressDialog progressDialog;
// profileUrl,username,memer
String reel_url;
Uri videouri;
FirebaseUser user;
FirebaseAuth auth;
DatabaseReference userRef,reelRef;
DatabaseReference databaseReference;
StorageReference storageReference;
private long counterReel=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reel);
init();
auth=FirebaseAuth.getInstance();
user=auth.getCurrentUser();
vtitle=(EditText)findViewById(R.id.vtitle);
storageReference= FirebaseStorage.getInstance().getReference().child("Reels");
userRef= FirebaseDatabase.getInstance().getReference().child("Users");
reelRef=FirebaseDatabase.getInstance().getReference("Reels");
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(AddReel.this,new String[]
{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
// getData();
browse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("video/*");
startActivityForResult(intent,1);
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadReel();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==RESULT_OK)
{
if (data !=null)
{
videouri=data.getData();
videoView.setVideoURI(videouri);
}
}else
{
Toast.makeText(this, "Something went wrong!!", Toast.LENGTH_SHORT).show();
}
}
private void uploadReel()
{
progressDialog=new ProgressDialog(AddReel.this);
progressDialog.setTitle("New Post");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
if (videouri == null)
{
Toast.makeText(this, "No Meme select!!", Toast.LENGTH_SHORT).show();
}else
{
final StorageReference sRef=storageReference.child(System.currentTimeMillis()+"."+getExtension(videouri));
sRef.putFile(videouri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
sRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
reel_url=uri.toString();
//database
// SharedPreferences.Editor editor=getSharedPreferences("post",MODE_PRIVATE).edit();
// editor.putString("id",postid);
// editor.apply();
saveReelsDataInFirebase(reel_url);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(AddReel.this, "Error"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(@NonNull UploadTask.TaskSnapshot snapshot) {
double progress=(100*snapshot.getBytesTransferred()/snapshot.getTotalByteCount());
progressDialog.setMessage("Uploaded "+((int)progress) +"%...");
}
});
}
}
private void init()
{
videoView=(VideoView)findViewById(R.id.videoView);
upload=(Button)findViewById(R.id.upload);
browse=(Button)findViewById(R.id.browse);
mediaController=new MediaController(this);
videoView.setMediaController(mediaController);
videoView.start();
}
public String getExtension(Uri videouri)
{
MimeTypeMap mimeTypeMap=MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(getContentResolver().getType(this.videouri));
}
private void saveReelsDataInFirebase(final String postReel)
{
reelRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists())
{
counterReel=snapshot.getChildrenCount();
}else
{
counterReel=0;
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
userRef.child(user.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists())
{
String n=snapshot.child("username").getValue().toString();
String m=snapshot.child("memer").getValue().toString();
String p=snapshot.child("profileUrl").getValue().toString();
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("dd-M-yyyy hh:mm a");
String currentDate=format.format(date);
String reelid=reelRef.push().getKey();
HashMap<String,Object> map=new HashMap<>();
map.put("date",currentDate);
map.put("reelid",reelid);
map.put("title",vtitle);
map.put("postReel",postReel);
map.put("publisher",user.getUid());
map.put("profile",p);
map.put("memer",m);
map.put("username",n);
map.put("counterPost",counterReel);
progressDialog.dismiss();
Toast.makeText(AddReel.this, "New post added!!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddReel.this,HomeActivity.class));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
型号class:-
Filemodel.java
public class Filemodel {
private String date;
private String reelid;
private String title;
private String postReel;
private String publisher;
private String profile;
private String memer;
private String username;
private long counterReel;
public Filemodel(String date, String reelid, String title, String postReel, String publisher, String profile, String memer, String username, long counterReels) {
this.date = date;
this.reelid = reelid;
this.title = title;
this.postReel = postReel;
this.publisher = publisher;
this.profile = profile;
this.memer = memer;
this.username = username;
this.counterReel = counterReels;
}
public Filemodel()
{
}
public String getDate() {
return date;
}
public String getreelid() {
return reelid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getpostReel() {
return postReel;
}
public void setpostreel(String postreel) {
this.postReel = postreel;
}
public String getPublisher() {
return publisher;
}
public String getProfile() {
return profile;
}
public String getMemer() {
return memer;
}
public String getUsername() {
return username;
}
public long getCounterPost() {
return counterReel;
}
}
第一步应该是停止忽略错误并实施 onCancelled
。至少应该是:
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
除此之外,我在您共享的代码中没有看到任何对 setValue
或 updateChildren
的调用,它们是唯一将数据写入数据库的调用。所以你可能想看看 writing data.