这是我的RegistrationActivity.java
,我希望用户在注册时间时选择图像。一切正常,除了图像更新!当我注册新用户并单击图像时,它会打开我的图像库,选择任何图像,然后它会重新注册活动,但不会更新。我也无法注册,因为该图片上传失败,我坐了下来(如果
imagepath == null
),这就是为什么会出现吐司错误,其中包含所有细节的原因。
注意:
1.我尝试了多个图像,同样的问题,同时我调整了一些图像的大小并尝试了。
2.logcat中没有错误。
3.当我们第一次尝试注册那个时候,我坐在默认的android logo上,默认图像完美显示。
4.我不能不选择任何图像进行注册,因为imagepath == null
,并且我确实希望该用户如果不上传图像也不要注册。
5.我坐着imageview尺寸:
android:layout_width="150dp"
android:layout_height="150dp"
在我的activity_registration.xml文件中。有什么问题吗?
package com.jimmytrivedi.loginapp;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.IOException;
public class RegistrationActivity extends AppCompatActivity {
private EditText userName, userEmail, userPassword, userAge;
private Button SignUp;
private TextView userLogin;
private FirebaseAuth firebaseAuth;
private ImageView profile;
private FirebaseStorage firebaseStorage;
private static int PICK_IMAGE = 123;
String name, email, password, age;
Uri imagePath;
private StorageReference storageReference;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && requestCode == RESULT_OK && data.getData() != null) {
imagePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imagePath);
profile.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
setUpUIViews();
firebaseAuth = FirebaseAuth.getInstance();
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
SignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (validate()) {
//upload data to the database
String user_email = userEmail.getText().toString().trim();
String user_password = userPassword.getText().toString().trim();
firebaseAuth.createUserWithEmailAndPassword(user_email, user_password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
sendEmailVerification();
} else {
Toast.makeText(RegistrationActivity.this, "Registration failed!", Toast.LENGTH_SHORT)
.show();
}
}
});
}
}
});
userLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
}
});
}
private void setUpUIViews() {
userName = findViewById(R.id.userName);
userEmail = findViewById(R.id.userEmail);
userPassword = findViewById(R.id.userPassword);
SignUp = findViewById(R.id.userRegister);
userLogin = findViewById(R.id.userLogin);
userAge = findViewById(R.id.userAge);
profile = findViewById(R.id.profile);
profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE);
}
});
}
private boolean validate() {
Boolean result = false;
name = userName.getText().toString();
email = userEmail.getText().toString();
password = userPassword.getText().toString();
age = userAge.getText().toString();
if (name.isEmpty() || password.isEmpty() || email.isEmpty() || age.isEmpty()
|| imagePath == null) {
Toast.makeText(this, "Please enter all the details", Toast.LENGTH_SHORT)
.show();
} else {
result = true;
}
return result;
}
private void sendEmailVerification() {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
firebaseUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
sendUserData();
Toast.makeText(RegistrationActivity.this, "Successfully registered, Verification email has been sent",
Toast.LENGTH_SHORT).show();
firebaseAuth.signOut();
finish();
startActivity(new Intent(RegistrationActivity.this, MainActivity.class));
} else {
Toast.makeText(RegistrationActivity.this, "Verification email hasn't been sent",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
private void sendUserData() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myReference = firebaseDatabase.getReference(firebaseAuth.getUid());
StorageReference imageReference = storageReference.
child(firebaseAuth.getUid()).child("Images").child("Profile Pic");
UploadTask uploadTask = imageReference.putFile(imagePath);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegistrationActivity.this, "Upload failed", Toast.LENGTH_SHORT)
.show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(RegistrationActivity.this, "Upload successful", Toast.LENGTH_SHORT)
.show();
}
});
UserProfile userProfile = new UserProfile(age, email, name);
myReference.setValue(userProfile);
}
}
答案 0 :(得分:0)
以此替换您的sendUserData
private void sendUserData() {
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference myReference = firebaseDatabase.getReference(firebaseAuth.getUid());
StorageReference imageReference = storageReference.
child(firebaseAuth.getUid()).child("Images").child("Profile Pic");
imageReference.putFile(imagePath).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(RegistrationActivity.this, "Upload failed", Toast.LENGTH_SHORT)
.show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(RegistrationActivity.this, "Upload successful", Toast.LENGTH_SHORT)
.show();
}
});
UserProfile userProfile = new UserProfile(age, email, name);
myReference.setValue(userProfile);
}