Firebase存储在上载到数据库期间出错

时间:2018-11-01 20:57:25

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

我正在尝试使用Firebase存储,但是在上载并将其发送到数据库时,我始终遇到致命错误。上载成功,但我尝试将下载网址获取到我的数据库,但出现此错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.apps.ayodkay.services, PID: 10693
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
    at com.apps.ayodkay.services.Profile$9.onComplete(Profile.java:550)
    at com.google.android.gms.tasks.zzj.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5753)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

这是我在Firebase存储中的文档中使用的代码

if (mImageUri != null) {

        Date date = new Date();

        long time = date.getTime();

        Uri file = Uri.fromFile(new File("path/to/images/" + time));

        final StorageReference fileReference = mStorageRef.child("images/" + file.getLastPathSegment());
        mUploadTask = fileReference.putFile(mImageUri);

        // Register observers to listen for when the download is done or if it fails
        mUploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
                // ...
            }
        });


        Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw Objects.requireNonNull(task.getException());
                }

                // Continue with the task to get the download URL
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    String uri = downloadUri.toString();
                    mUsernameDatabase.child("image").setValue(uri);
                    Toast.makeText(Profile.this, "upload Done", Toast.LENGTH_SHORT).show();
                    mprogress.setVisibility(View.GONE);

                } else {
                    Toast.makeText(Profile.this, "Error uploaing", Toast.LENGTH_SHORT).show();
                }
            }
        });
        // [END upload_get_download_url]
    }

1 个答案:

答案 0 :(得分:1)

我尝试使用另一种方法,效果很好。我使用了UserProfileChangeRequest方法将其发送到我的数据库,并设置了一个新的用户照片网址

 Task<Uri> urlTask = mUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (!task.isSuccessful()) {
                    throw Objects.requireNonNull(task.getException());
                }

                // Continue with the task to get the download URL
                return fileReference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    FirebaseUser user = mAuth.getCurrentUser();

                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                            .setPhotoUri(downloadUri)
                            .build();
                    user.updateProfile(profileUpdates).addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(Profile.this, "upload Done", Toast.LENGTH_SHORT).show();
                            mprogress.setVisibility(View.GONE);
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                        }
                    });


                } else {
                    Toast.makeText(Profile.this, "Error uploaing", Toast.LENGTH_SHORT).show();
                }
            }
        });