【问题标题】:Firebase storage is error during upload to databaseFirebase 存储在上传到数据库期间出错
【发布时间】:2018-11-01 20:57:25
【问题描述】:

我正在尝试使用 firebase 存储,但是当我上传并想将其发送到我的数据库时,我不断收到致命错误。上传成功,但我尝试将下载 url 获取到我的数据库,但出现此错误

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]
    }

【问题讨论】:

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


    【解决方案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();
                    }
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2021-07-12
      • 1970-01-01
      • 2020-08-09
      • 2018-01-12
      • 2019-08-29
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多