如何在Firebase数据库中存储多个图像Url?

时间:2018-05-22 09:30:16

标签: java android firebase firebase-realtime-database firebase-storage

我已经在firebase存储中上传了多个图片,现在我想在数据中添加他们的网址以及其他数据。我该怎么做?我在日志中获取所有网址但是当我尝试使用以下代码时应用程序崩溃了?

storageReference = FirebaseStorage.getInstance().getReference();
        databaseReference= FirebaseDatabase.getInstance().getReference().child("Blog Data");

final DatabaseReference newBlog = databaseReference.push();
 for (int i = 0; i < list.size(); i++)

        {
            if (imageUri != null) {

                final StorageReference ref = filePath.child(fileName[i]);

                final int y = i;
                ref.putFile(imageUri[i])
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                                  @Override
                                                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                                      progressDialog.cancel();
                                                      Toast.makeText(getApplicationContext(), "Uploaded successfuly", Toast.LENGTH_LONG).show();
                                                   downloadUri[y] = taskSnapshot.getDownloadUrl();
                                                      Log.i("DownlaodUri ", "downlaod URi is :"+downloadUri[y].toString());

                                                     newBlog.child("image_"+y+1).setValue(downloadUri[y]);




                                                  }


                                              }

                        )

我还尝试在第一次循环后使用另一个for循环来存储URL。它将所有数据保存在数据库中,但不存储URL。

这是我尝试的方式。

}// end of upper for loop
       DatabaseReference newBlog = databaseReference.push();
        newBlog.child("title").setValue(title);
        newBlog.child("desc").setValue(desc);


        for(int i =  0; i<list.size(); i++) {

           newBlog.child("image_"+i+1).setValue(downloadUri[i]);
       }
       newBlog.child("location").setValue(location);
  

它存储除图像之外的所有值。

将所有图像网址存储在数据库中之后我应该如何获取所有图像,因为我不知道数据库中的图像数量,所以我无法获取一定数量的imageview?我如何知道一个节点有多少个子名称“ïmages_1”,“images_2”?

logcat的。

identical 617 lines
05-22 15:07:22.736 29586-29586/com.example.anant.ExampleE/UncaughtException:     at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                    at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                    at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                    at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                    at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                    at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                    at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)



I/zygote64: Background concurrent copying GC freed 635684(49MB) AllocSpace objects, 1(20KB) LOS objects, 44% free, 29MB/53MB, paused 377us total 111.410ms
05-22 15:07:22.737 29586-29586/com.example.anant.ExampleE/UncaughtException:     at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                    at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                    at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                    at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)

I/chatty: uid=10162(com.example.anant.Example) identical 1231 lines
05-22 15:07:23.524 29586-29586/com.example.anant.ExampleE/AndroidRuntime:     at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)

FATAL EXCEPTION: main
                                                                             Process: com.example.anant.Example, PID: 29586
                                                                             java.lang.StackOverflowError: stack size 8MB
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:128)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)
                                                                                 at com.google.android.gms.internal.zzekq.zzca(Unknown Source:132)
                                                                                 at com.google.android.gms.internal.zzekp.zzbw(Unknown Source:263)
                                                                                 at com.google.android.gms.internal.zzekp.zzbz(Unknown Source:0)

2 个答案:

答案 0 :(得分:0)

storageReference = FirebaseStorage.getInstance().getReference();
    databaseReference= FirebaseDatabase.getInstance().getReference().child("Blog Data");

//final DatabaseReference newBlog = databaseReference.push();//not required
 for (int i = 0; i < list.size(); i++)

    {
        if (imageUri != null) {

            final StorageReference ref = filePath.child(fileName[i]);

            final int y = i;
            ref.putFile(imageUri[i])
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                              @Override
                                              public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                                  progressDialog.cancel();
                                                  Toast.makeText(getApplicationContext(), "Uploaded successfuly", Toast.LENGTH_LONG).show();
                                               downloadUri[y] = taskSnapshot.getDownloadUrl();
                                                  Log.i("DownlaodUri ", "downlaod URi is :"+downloadUri[y].toString());

        HashMap map = new HashMap();
        map.put("image_url",downloadUri[y]);
                                                 databaseReference.push().updateChildren(map);




                                              }


                                          }

                    )

请尝试编辑上面的代码并尝试..

答案 1 :(得分:0)

我忘了在将downloadUri传递给setvalue()时添加.toString。这是造成overFlow异常的原因。

详情请参阅此问题

  

https://stackoverflow.com/a/38128667/8457096

PS。我不知道为什么人们无法回答问题,如果他们无法回答它。