Add multiple pictures into Firebase.

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

Add multiple pictures into firebase

问题

我知道这个问题已经被问了很多次。但是实际上,没有一个答案对我有用。
我的问题是图像似乎已经保存在Firebase上,但不幸的是它们都具有相同的名称,因此Firebase会删除旧的图像并插入新的图像。我已经尽力更改名称,但是找不到方法。

这是我用于上传的代码!

英文:

I know this question has been asked a lot. But literally non of the answers work for me.
My problem is the images appeared to be saved but on firebase but unfortunately they are the same name so firebase deletes the old one and a inserts a new one. I have done everything i can do to change the name but I can't find a way.

Here is my code for uploading!

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();
                        }
                    });

        }





    }

Any ideas?

答案1

得分: 0

你只在所有图像中计算一次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)
        ...
英文:

You're only calculating imageid once across all images, so each image will have the same name. You'll want to calculate a new value for each image, and probably also throw in a counter just to make sure they're all unique:

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)
        ...

huangapple
  • 本文由 发表于 2020年8月12日 08:48:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63368221.html
匿名

发表评论

匿名网友

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

确定