图片未上传到数据库 [英] Image not uploaded to Database

查看:75
本文介绍了图片未上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像上传到Firebase数据库。设备使用相机单击图像,该图像应上传到数据库。此代码可以正常运行,但不会将任何图片上传到数据库。当我运行该应用程序时,它将带到可以成功单击图像的相机上,但是此后,它不显示进度栏,并且图像也不会上传。

I am trying to upload the image to the firebase database. The device clicks an image using the camera and that should be uploaded to the database. This code runs without any errors but it doesn't upload any image to the database. when I run this application it takes to the camera where I'm successfully able to click the image but after that, it doesn't show the progress bar and also the image doesn't upload.

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;


public class New extends AppCompatActivity {
    private ListView lst;
    private Button btn;
    private StorageReference mstr;
    private final static int GALLERY_INTENT=2;
    private final static int CAMERA_REQUEST_CODE=1;
    private ProgressBar progressBar;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        //to upload images to database
        mstr = FirebaseStorage.getInstance().getReference();
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        img = findViewById(R.id.imageView);

        btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(intent,CAMERA_REQUEST_CODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
            progressBar.setVisibility(View.VISIBLE);
            Uri uri = data.getData();

            StorageReference filepath = mstr.child("Photos").child(uri.getLastPathSegment());

            filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(New.this,"Upload done",Toast.LENGTH_LONG).show();
                    Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl();
                    Picasso.with(New.this).load(String.valueOf(downloadUri)).fit().centerCrop().into(img);
                    progressBar.setVisibility(View.INVISIBLE);
                }
            });
        }
    }
}


推荐答案

使用此命令:-

StorageReference filepath = mstr.child("Photos").child(uri.getLastPathSegment());

UploadTask uploadTask = filepath.putFile(uri);


uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                            if (!task.isSuccessful()) {
                                throw task.getException();
                            }
                            return storageReference.getDownloadUrl();
                        }
                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            videoDownloadUrl = String.valueOf(task.getResult());

                            final Map<String,Object> map = new HashMap<>();
                            map.put("URL",videoDownloadUrl);

                            db.child("").child("").push().setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    //do what ever you want.
                        }
                    });

这篇关于图片未上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆