Reputation:
I am fairly new to Android and want to make an app that allows users to upload pictures using Firebase storage. After they log in, I made it so that their Uid gets sent to SharedPreferences, so that I can retrieve it in my imageupload activity later. I need it so that when the image gets sent to Firebase, it gets put under the folder of their userid.
This is my current login activity that retrieves the 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();
And this is my imageupload activity:
sharedPref = getPreferences(MODE_PRIVATE);
String UserId = sharedPref.getString("firebasekey", "1");
StorageReference storageRef= FirebaseStorage.getInstance().getReference();
StorageReference mountainsRef = storageRef.child("uploads/"+UserId+System.currentTimeMillis());
Help would be greatly appreciated.
Upvotes: 1
Views: 3485
Reputation: 5148
Here you go
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) {
}
});
And to retrieve image
StorageReference storageReference = FirebaseStorage.getInstance().getReference("newFolder/" + userUid);
You can read here more about FirebaseCloudStorage. Here how to upload, and here how to download them.
Upvotes: 5
Reputation: 2809
Hi Mozy below code should, when called launch intent to choose an image from gallery and upload it to firebase. Hope this helps.
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
}
});
}
Upvotes: 0