如何为Firebase用户android更新个人资料图片?

时间:2018-09-22 23:09:49

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

我有一个具有用户名,电子邮件和个人资料图片的用户,但是我在注册时将个人资料图片设置为空字符串。我正在尝试更新个人资料照片并使用当前用户进行设置,但出现错误。

mProfilePic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY_INTENT);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK && data != null && data.getData() != null){
        mainImageURI = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), mainImageURI);
            mProfilePic.setImageBitmap(bitmap);
            final ProgressDialog proDialog = new ProgressDialog(HomeActivity.this);
            proDialog.setTitle("Uploading...");
            proDialog.show();

            final StorageReference mRef = mStorage.child("Photos").child(mainImageURI.getLastPathSegment());
            mRef.putFile(mainImageURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Task<Uri> uri = taskSnapshot.getMetadata().getReference().getDownloadUrl();
                    Log.i("uploaded", mDatabase.getReference("Users").child(mAuth.getCurrentUser().getUid()).child("image").setValue(uri).toString());
                    mDatabase.getReference("Users").child(mAuth.getCurrentUser().getUid()).child("image").setValue(uri).toString();
                    proDialog.dismiss();
                    Toast.makeText(HomeActivity.this, "Uploaded", Toast.LENGTH_LONG).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    proDialog.dismiss();
                    Toast.makeText(HomeActivity.this, "Upload Failed "+e.getMessage(), Toast.LENGTH_LONG).show();
                }
            })
            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
                    proDialog.setMessage("Uploaded "+(int)progress+"%");
                }
            });
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

如果我删除了与用户连接有关的任何内容,则该代码有效。

这是我得到的错误:

09-22 23:12:05.896 20706-20706/com.example.kisha.androidapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kisha.androidapp, PID: 20706
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:130)
        at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
        at com.google.android.gms.internal.firebase_database.zzkt.zzh(Unknown Source:0)
        at com.google.firebase.database.DatabaseReference.zza(Unknown Source:10)
        at com.google.firebase.database.DatabaseReference.setValue(Unknown Source:7)
        at com.example.kisha.androidapp.HomeActivity$4.onSuccess(HomeActivity.java:129)
        at com.example.kisha.androidapp.HomeActivity$4.onSuccess(HomeActivity.java:125)
        at com.google.firebase.storage.zzi.zza(Unknown Source:13)
        at com.google.firebase.storage.zzac.zza(Unknown Source:2)
        at com.google.firebase.storage.zzaf.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Users->(uid)->用户名,电子邮件,图像 当用户注册时,所有字段均为字符串,而image为null

1 个答案:

答案 0 :(得分:0)

这是更新个人资料图片的方法,然后可以基于mFirebaseUser对其进行调用。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("Jane Q. User")
        .setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg"))
        .build();

user.updateProfile(profileUpdates)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User profile updated.");
                }
            }
        });