我是Android的新手,想要创建一个允许用户使用Firebase存储上传图片的应用。在他们登录后,我做了它,以便他们的Uid被发送到SharedPreferences,以便我可以在我的imageupload活动中检索它。我需要它,以便在将图像发送到Firebase时,它会被置于其用户标识的文件夹下。
这是我当前的检索Uid的登录活动:
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
String userId = firebaseUser.getUid();
sharedPref = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("firebasekey", userId);
editor.commit();
这是我的imageupload活动:
sharedPref = getPreferences(MODE_PRIVATE);
String UserId = sharedPref.getString("firebasekey", "1");
StorageReference storageRef= FirebaseStorage.getInstance().getReference();
StorageReference mountainsRef = storageRef.child("uploads/"+UserId+System.currentTimeMillis());
非常感谢帮助。
答案 0 :(得分:3)
你去吧
String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
UploadTask uploadTask = FirebaseStorage.getInstance().child("newFolder").getReference(userUid).putBytes(yourPhotoToByteArray);
uploadTask
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
并检索图像
StorageReference storageReference = FirebaseStorage.getInstance().getReference("newFolder/" + userUid);
答案 1 :(得分:0)
Hi Mozy下面的代码应该在调用启动意图时从库中选择一个图像并将其上传到firebase。希望这会有所帮助。
private void updateImg() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select picture"), CHOOSE_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == Activity.RESULT_OK) {
if (data == null) {
// Display an error
return;
}
// create an inputStream from the intent result we have just created
InputStream inputStream = null;
try {
inputStream = yourActivity.this.getContentResolver().openInputStream(data.getData());
uploadImageToFirebase(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private void uploadImageToFirebase(InputStream rInputStream) {
sharedPref = getPreferences(MODE_PRIVATE);
String UserId = sharedPref.getString("firebasekey", "1");
StorageReference storageRef =
FirebaseStorage.getInstance().getReference();
StorageReference mountainsRef = storageRef.child("uploads/"+UserId+System.currentTimeMillis());
// running the task
UploadTask uploadTask = mountainsRef.putStream(rInputStream);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// show error
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// do ui stuff
}
});
}