我正在尝试使用Firebase将上传的照片与用户相关联,以便在他们的个人资料中看到它们。目前它正在将它们上传到存储,但它似乎不一定与用户ID相关联。他们不应该在"数据库"部?
我查看了https://firebase.google.com/docs/storage/android/upload-files以及https://firebase.google.com/docs/storage/android/download-files和一些stackoverflow文章(Firebase storage structure example)和youtube vids
我特别尝试的是让getCurentUser()将其设置为String并将其附加到图像名称,以便稍后我可以找到它,但我假设必须有一个更简单的内置的方式?
我登录然后转到RegisterPhotoActivity:
public class RegisterPhotoActivity extends Activity {
Button uploadProfilePhoto;
ImageView checkmarkImage, backArrowImage;
private FirebaseAuth auth;
private static FirebaseUser currentUser;
String currentUserString;
private static final int SELECTED_PICTURE = 1;
FirebaseStorage storage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registerphoto);
uploadProfilePhoto = (Button) findViewById(R.id.uploadPhotoBTN);
checkmarkImage = (ImageView) findViewById(R.id.checkmarkImage);
backArrowImage = (ImageView) findViewById(R.id.leftArrow);
backArrowImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(RegisterPhotoActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
auth = FirebaseAuth.getInstance();
currentUser =
auth.getCurrentUser();
currentUserString = currentUser.toString();
Log.i("CurrentUserString", currentUserString);
storage = FirebaseStorage.getInstance();
//storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child("20170702_174811.jpeg");
//storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child(currentUserString);
storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com/images").child(currentUserString);
checkmarkImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(RegisterPhotoActivity.this, RegisterBusinessActivity.class);
startActivity(intent);
}
});
uploadProfilePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
handleChooseImage(view);
}
});
}
//END OF onCreate
//Separate methods
//Actually opens the CameraRoll
public void handleChooseImage(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECTED_PICTURE); //then goes to onActivityResult
}
public void handleInsertData(View v) {
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if(requestCode == RESULT_OK) {
Log.i("RegisterActivity", "case 0");
}
break;
case 1:
if(resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
Log.i("RegisterActivity", "selected image = " + selectedImage);
Bitmap imageBitmap = null;
try {
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
encodeBitmapAndSaveToFirebase(imageBitmap);
}
break;
}
}
public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //was PNG
byte[] data = baos.toByteArray();
UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(RegisterPhotoActivity.this, "reached onSuccess:", Toast.LENGTH_SHORT).show();
}
});
}
}
以下是我的存储规则:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
match /images {
// Only an individual user can write to "their" images
match /{userId}/{imageId} {
allow write: if request.auth.uid == userId;
}
}
}
}
答案 0 :(得分:0)
将照片上传到存储时,数据库中不会自动发生任何事情。一种选择是将onSuccess
中的一些代码添加到OnSuccessListener
中,获取照片的下载URL,然后将其写入相关员工用户ID下的数据库。代码可能类似于:
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Map<String, Object> map = new HashMap<>();
map.put("photoDownloadUrl", downloadUrl.toString());
userRef.updateChildren(map);