2017-10-17 191 views
0

我想添加一個圖像到Android的實時數據庫(firebase)中的用戶信息。我已經將圖片上傳到firebase存儲中,但我將如何將該圖片添加到該用戶的數據庫中?上傳用戶個人資料圖片Firebase

下面的代碼:

//inside onCreate() method 

img.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i=new Intent(Intent.ACTION_PICK); 
      i.setType("image/*"); 
      startActivityForResult(i,request_code); 
     } 
    }); 

在這裏,我點擊ImageView的,所以我將能夠從畫廊改變它,得到的圖像。

在這裏,我認證用戶併發送數據到數據庫中:

auth.createUserWithEmailAndPassword(email, password) 
        .addOnCompleteListener(StudentSignUpActivity.this, new OnCompleteListener<AuthResult>() { 
         @Override 
         public void onComplete(@NonNull Task<AuthResult> task) { 
          Toast.makeText(getApplicationContext(), "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show(); 
          progressBar.setVisibility(View.GONE); 
          // If sign in fails, display a message to the user. If sign in succeeds 
          // the auth state listener will be notified and logic to handle the 
          // signed in user can be handled in the listener. 
          if (!task.isSuccessful()) { 
           Toast.makeText(getApplicationContext(), "Authentication failed." + task.getException(), 
             Toast.LENGTH_SHORT).show(); 
          } else { 
           startActivity(new Intent(StudentSignUpActivity.this, HomeActivity.class)); 
           finish(); 
          } 
         } 
        }); 

mCurrentUser=FirebaseAuth.getInstance().getCurrentUser(); 
      DatabaseReference newStudent=mDatabase.push(); 
      newStudent.child("email").setValue(email); 
      newStudent.child("password").setValue(password); 
      newStudent.child("name").setValue(name); 
      newStudent.child("date").setValue(dates); 
      newStudent.child("phone").setValue(number); 
      newStudent.child("uid").setValue(mCurrentUser.getUid()); 


//outside of onCreate() 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode==request_code&&resultCode==RESULT_OK){ 
     Uri uri=data.getData(); 
     StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment()); 
     filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 

      } 
     }); 
    } 
} 

在上面的代碼中,我已上傳的圖像到火力存儲。現在,我將如何將該圖像添加爲特定用戶的孩子。

我想我需要做這樣的事情:

newStudent.child("image").setValue(uri_here); 

但我無法弄清楚如何獲取圖像的URI,以及如何添加在setValue()該URI,因爲它在另一種方法。

回答

1

您可以使用該方法getDownloadUrl()在成功監聽器訪問下載網址:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if(requestCode==request_code&&resultCode==RESULT_OK){ 
     Uri uri=data.getData(); 
     StorageReference filepath=mStorage.child("Images").child(uri.getLastPathSegment()); 
     filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
       Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
       newStudent.child("image").setValue(downloadUrl); 
      } 
     }); 
    } 
} 

順便說一句,而不是使用push(),我建議用uid爲關鍵存儲用戶的數據。這將使您的數據更容易找到。

private DatabaseReference newStudent; 

mCurrentUser=FirebaseAuth.getInstance().getCurrentUser(); 
      newStudent=mDatabase.child(mCurrentUser.getUid()); 
      newStudent.child("email").setValue(email); 
      // etc 
+0

其實'newStudent = mDatabase.child(mCurrentUser.getUid())'不起作用其返回零點例外,因爲沒有在尚未簽署用戶。上面的代碼用於註冊 –

相關問題