我该如何上传和接收用户个人资料图片?
How can I go about uploading and receiving a users profile picture?
到目前为止,在我的社交媒体应用中,用户的名字、姓氏、电子邮件、性别等数据可以保存在 firebase 数据库中,并在需要时检索。从今天开始,我在第一次创建个人资料时得到了一张可用的个人资料图片,您可以点击空的个人资料图片图标,它会加载您的画廊,用用户选择的任何图像替换它。
尽管这非常简洁,但我需要能够以某种方式在我的 firebase 数据库的用户节点下上传这张图片。在转换位图数据时我很迷茫,在阅读了一些文档后它仍然让我感到困惑。下面是我使用本地保存的照片将其替换为个人资料图片以显示我目前所拥有的照片的代码。
@Override
public void onClick(View view)
{
if (view == profilePicture)
{
//Toast.makeText(this, "We made it to the onClick for image!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri targetUri = data.getData();
Bitmap bitmap;
try
{
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
profilePicture.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
onClick 方法在用户点击个人资料图标后运行。现在我将向您展示我目前正在使用的数据库,这是 Firebase 实时数据库,而不是 Firebase 存储。虽然 firebase 存储可能更合适,但我似乎无法弄清楚如何判断谁的照片是谁,因为它不会使用与之关联的用户 ID 上传它们。
Database Picture Here
对于像您这样的简单问题,更好的解决方案是 Firebase 存储,而不是上传位图,它类似于数据库,但您可以轻松上传图像等文件。
以下是我目前在我的应用中使用的方法:
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
private void uploadImage() {
if(filePath != null)
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/"+userID);
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
toastMessage("Uploaded");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
toastMessage("Failed"+e.getMessage());
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}
到目前为止,在我的社交媒体应用中,用户的名字、姓氏、电子邮件、性别等数据可以保存在 firebase 数据库中,并在需要时检索。从今天开始,我在第一次创建个人资料时得到了一张可用的个人资料图片,您可以点击空的个人资料图片图标,它会加载您的画廊,用用户选择的任何图像替换它。
尽管这非常简洁,但我需要能够以某种方式在我的 firebase 数据库的用户节点下上传这张图片。在转换位图数据时我很迷茫,在阅读了一些文档后它仍然让我感到困惑。下面是我使用本地保存的照片将其替换为个人资料图片以显示我目前所拥有的照片的代码。
@Override
public void onClick(View view)
{
if (view == profilePicture)
{
//Toast.makeText(this, "We made it to the onClick for image!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri targetUri = data.getData();
Bitmap bitmap;
try
{
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
profilePicture.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
onClick 方法在用户点击个人资料图标后运行。现在我将向您展示我目前正在使用的数据库,这是 Firebase 实时数据库,而不是 Firebase 存储。虽然 firebase 存储可能更合适,但我似乎无法弄清楚如何判断谁的照片是谁,因为它不会使用与之关联的用户 ID 上传它们。
Database Picture Here
对于像您这样的简单问题,更好的解决方案是 Firebase 存储,而不是上传位图,它类似于数据库,但您可以轻松上传图像等文件。
以下是我目前在我的应用中使用的方法:
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
private void uploadImage() {
if(filePath != null)
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/"+userID);
ref.putFile(filePath)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
toastMessage("Uploaded");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
toastMessage("Failed"+e.getMessage());
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded "+(int)progress+"%");
}
});
}
}