将多张图片添加到Firebase

问题描述

我知道这个问题已经问了很多。但实际上没有答案对我有用。 我的问题是图像似乎已保存但在Firebase上,但不幸的是它们的名称相同,因此firebase删除了旧图像,并插入了新图像。我已经做了一切可以更改名称的事情,但找不到办法。

这是我要上传代码

private void Fileuploader() throws FileNotFoundException {
        String imageid;
        imageid = arabic.getText().toString()+"-"+System.currentTimeMillis()+"."+"jpg";
        String id = getIntent().getStringExtra("storeid");
        member.setimageid(imageid);
        reference.child(id).child(arname).setValue(member);
        progress.showProgress(profuctadd.this,"Loading...",false);
        DatabaseHelper databaseHelper = new DatabaseHelper(profuctadd.this);
        Cursor getimage = databaseHelper.GetPath();
        while (getimage.movetoNext()){
            Bitmap bm = BitmapFactory.decodeFile(getimage.getString(0));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG,35,out);
            byte[] data = out.toByteArray();
            System.out.println("IMAGES UPLOADEdddd: "+ data);
            StorageReference Ref = mStorageRef.child(imageid);

            Ref.putBytes(data)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            // Get a URL to the uploaded content
                            //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) {
                            // Handle unsuccessful uploads
                            // ...
                            Toast.makeText(profuctadd.this,"Failed",Toast.LENGTH_LONG).show();
                        }
                    });

        }





    }

有什么想法吗?

解决方法

您在所有图像上只计算一次imageid,因此每个图像将具有相同的名称。您将需要为每个图像计算一个新值,并且可能还会抛出一个计数器,以确保它们都是唯一的:

private void Fileuploader() throws FileNotFoundException {
    String id = getIntent().getStringExtra("storeid");
    member.setImageid(imageid);
    reference.child(id).child(arname).setValue(member);
    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);
        byte[] data = out.toByteArray();
        System.out.println("IMAGES UPLOADEDDDD: "+ data);
        StorageReference Ref = mStorageRef.child(imageid);

        String imageid = arabic.getText().toString()+"-"+System.currentTimeMillis()+"_"+(count++)+"."+"jpg";
        Ref.putBytes(data)
        ...