从Firebase存储和实时数据库获取图像。

huangapple go评论99阅读模式
英文:

Get images from firebase storage and realtime database

问题

以下是要翻译的代码部分:

所以我想让用户同时上传多张图片到Firebase存储我已经成功完成了这部分但现在我想要检索这些图片起初我为每个用户上传一张图片并将图片ID保存到实时数据库中因此通过图片ID很容易将图片检索回来但现在我如何为每个用户保存多张图片到实时数据库中我不能使用相同的子节点名称因为它会被旧数据替代有什么想法吗

上传图片并在实时数据库上设置ID

private void Fileuploader() throws FileNotFoundException {
    String imageid;

    progress.showProgress(profuctadd.this, "Loading...", false);
    DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
    Cursor getimage = databaseHelper.GetPath();
    int count = 0;
    while (getimage.moveToNext()){
        Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 35, out);
        imageid = arabic.getText().toString() + "-" + System.currentTimeMillis() + "_" + (count++) + "." + getExtension(uri);
        System.out.println("IMAGES UPLOADEDDDD: " + imageid);
        String id = getIntent().getStringExtra("storeid");
        member.setImageid(imageid);
        reference.child(id).child(arname).setValue(member);
        byte[] data = out.toByteArray();
        StorageReference Ref = mStorageRef.child(imageid);
        Ref.putBytes(data)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // 获取上传内容的URL
                    //Uri downloadUrl = taskSnapshot.getDownloadUrl();
                    //Toast.makeText(profuctadd.this, "Image uploaded", Toast.LENGTH_LONG).show();
                    progress.hideProgress();
                    Intent intent = new Intent(profuctadd.this, showProducts.class);
                    intent.putExtra("spec", getIntent().getStringExtra("spec"));
                    intent.putExtra("storeid", getIntent().getStringExtra("storeid"));
                    startActivity(intent);
                    DatabaseHelper mDatabaseHelper = new DatabaseHelper(profuctadd.this);
                    Cursor cursor2 = mDatabaseHelper.DeleteDataOfTableImagesAr();
                    cursor2.moveToNext();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // 处理上传失败
                    // ...
                    Toast.makeText(profuctadd.this, "Failed", Toast.LENGTH_LONG).show();
                }
            });
    }
}

我的Firebase数据库:

从Firebase存储和实时数据库获取图像。

英文:

So, I wanna make the user upload multiple images at the same time to firebase storage. I have done that part successfully but now I want to retrieve the images back. At first, I was uploading one image per user and save the image id to the realtime database so it was very easy to retrieve the image back by the image id. But now how can I save more than one image to the realtime database per user I can't use the same child name because it will be replaced with the old one. Any ideas??

Uploding images and setting the id on realtime database:

private void Fileuploader() throws FileNotFoundException {
String imageid;
progress.showProgress(profuctadd.this,&quot;Loading...&quot;,false);
DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
Cursor getimage = databaseHelper.GetPath();
int count = 0;
while (getimage.moveToNext()){
Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
ByteArrayOutputStream out = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 35, out);
imageid = arabic.getText().toString()+&quot;-&quot;+System.currentTimeMillis()+&quot;_&quot;+(count++)+&quot;.&quot;+getExtension(uri);
System.out.println(&quot;IMAGES UPLOADEDDDD: &quot;+ imageid);
String id = getIntent().getStringExtra(&quot;storeid&quot;);
member.setImageid(imageid);
reference.child(id).child(arname).setValue(member);
byte[] data = out.toByteArray();
StorageReference Ref = mStorageRef.child(imageid);
Ref.putBytes(data)
.addOnSuccessListener(new OnSuccessListener&lt;UploadTask.TaskSnapshot&gt;() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
//Uri downloadUrl = taskSnapshot.getDownloadUrl();
//Toast.makeText(profuctadd.this,&quot;Image uploaded&quot;,Toast.LENGTH_LONG).show();
progress.hideProgress();
Intent intent = new Intent(profuctadd.this,showProducts.class);
intent.putExtra(&quot;spec&quot;,getIntent().getStringExtra(&quot;spec&quot;));
intent.putExtra(&quot;storeid&quot;,getIntent().getStringExtra(&quot;storeid&quot;));
startActivity(intent);
DatabaseHelper mDatabaseHelper = new DatabaseHelper(profuctadd.this);
Cursor cursor2 = mDatabaseHelper.DeleteDataOfTableImagesAr();
cursor2.moveToNext();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
// ...
Toast.makeText(profuctadd.this,&quot;Failed&quot;,Toast.LENGTH_LONG).show();
}
});
}
}

My firebase db:

从Firebase存储和实时数据库获取图像。

答案1

得分: 0

你可以保存特定用户的多张图像在像这样的图像节点中。

DatabaseReference dr = FirebaseDatabase.getInstance().getReference("tL131");
dr.child("images").push().setValue(String.ValueOf(taskSnapshot.getDownloadUrl()));
英文:

you can save more than one image of specific user in images node like that.

DatabaseReference dr =  FirebaseDatabase.getInstance().getReference(&quot;tL131);
dr.child(&quot;images&quot;).push().setValue(String.ValueOf(taskSnapshot.getDownloadUrl()));

huangapple
  • 本文由 发表于 2020年8月13日 04:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63384389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定