2017-10-17 16 views
0

アンドロイドのリアルタイムデータベース(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の上でクリックしていますので、私はそれを変更し、ギャラリーから画像を取得することができます。ここで

私は、ユーザーを認証し、データベースにデータを送信:私はfirebaseストレージに画像をアップロードした上記のコードで

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

あなたは、ダウンロードURLにアクセスするための成功リスナー方法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で、ユーザーのデータを格納することをお勧めします。これにより、データを簡単に見つけることができます。

+0

private DatabaseReference newStudent; mCurrentUser=FirebaseAuth.getInstance().getCurrentUser(); newStudent=mDatabase.child(mCurrentUser.getUid()); newStudent.child("email").setValue(email); // etc 
まだログインユーザが存在しないので、実際に 'newStudent = mDatabase.child(mCurrentUser.getUid())'、その戻りヌル点例外動作しません。上のコードはサインアップのためのものです –

関連する問題