我有两个问题:
这是我的代码
public class JobSignup extends AppCompatActivity {
private Button btnChoose, btnUpload;
private ImageView imageView;
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;
//private EditText Email_editText , Password_editText , Name_editText;
private Button Register_button ;
private EditText inName, inEmail, inPassword, inDateOfBirth, inAddress,
inRePassword,inExperiences,inQualification,inSkills;
private FirebaseAuth fAuth;
private DatabaseReference fUserDatabase;
private ProgressDialog progressDialog;
FirebaseStorage storage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_signup);
getSupportActionBar().hide();
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();
btnChoose = (Button) findViewById(R.id.btnChoose);
btnUpload = (Button) findViewById(R.id.btnUpload);
imageView = (ImageView) findViewById(R.id.iv);
Register_button = (Button) findViewById(R.id.Button);
inEmail = (EditText) findViewById(R.id.editEmail);
inName = (EditText) findViewById(R.id.editUsername);
inPassword = (EditText) findViewById(R.id.editPassword);
inRePassword = (EditText) findViewById(R.id.editConfirmPassword);
inDateOfBirth = (EditText) findViewById(R.id.editDateOfBirth);
inAddress = (EditText) findViewById(R.id.editAddress);
inExperiences = (EditText) findViewById(R.id.editExperiences);
inQualification = (EditText) findViewById(R.id.editQualification);
inSkills = (EditText) findViewById(R.id.editAdditionalSkills);
fAuth = FirebaseAuth.getInstance();
fUserDatabase = FirebaseDatabase.getInstance().getReference().child("User");
btnChoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage();
}
});
Register_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uEmail = inEmail.getText().toString().trim();
String uName = inName.getText().toString().trim();
String uPassword = inPassword.getText().toString().trim();
String uRePassword = inRePassword.getText().toString().trim();
String uDateOfBirth = inDateOfBirth.getText().toString().trim();
String uAddress = inAddress.getText().toString().trim();
String uExperiences = inExperiences.getText().toString().trim();
String uQualification=inQualification.
getText().toString().trim();
String uSkills = inSkills.getText().toString().trim();
register_user(uName,uEmail,uPassword,uRePassword,uAddress,
uDateOfBirth,uExperiences,uQualification,uSkills);
}
});
}
private void register_user(final String name, final String email, String
password,final String rePassword,final String Qualification,final String
Experiences,final String Skills,final String address , final String
DateOfBirth){
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Processing your data, Please wait . . .");
progressDialog.show();
fAuth.createUserWithEmailAndPassword(email,
password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
fUserDatabase.child(fAuth.getCurrentUser().getUid())
.child("name").setValue(name)
.addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful()){
progressDialog.dismiss();
fUserDatabase.child(fAuth.getCurrentUser().getUid()).
child("DateOfBirth").setValue(DateOfBirth);
fUserDatabase.child(fAuth.getCurrentUser().getUid()).child("Address").
setValue(address);
fUserDatabase.child(fAuth.getCurrentUser().getUid()).
child("Email").setValue(email);
fUserDatabase.child(fAuth.getCurrentUser().
getUid()).child("Qualification").setValue(Qualification);
fUserDatabase.child(fAuth.getCurrentUser().getUid()).child("Experiences").
setValue(Experiences);
fUserDatabase.child(fAuth.getCurrentUser().getUid()).child("Skills").
setValue(Skills);
Intent i = new Intent(JobSignup.this,
Login.class);
startActivity(i);
finish();
Toast.makeText(JobSignup.this, "User has
been created!", Toast.LENGTH_SHORT).show();
}else{
progressDialog.dismiss();
Toast.makeText(JobSignup.this, "Error: "+
task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}else{
progressDialog.dismiss();
Toast.makeText(JobSignup.this, "Error: "+
task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
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);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null )
{
filePath = data.getData();
try {
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private void uploadImage() {
if (filePath != null) {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
StorageReference ref = storageReference.child("images/" +
UUID.randomUUID().toString());
ref.putFile(filePath)
.addOnSuccessListener(new
OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot
taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(JobSignup.this, "Uploaded",
Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(JobSignup.this, "Failed " +
e.getMessage(), Toast.LENGTH_SHORT).show();
}
})
.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 + "%");
}
});
}
}
}
答案 0 :(得分:0)
确保您已将此权限添加到manifest
文件中。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
使用chosseImage
方法添加此行。
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
更新onActivityResult
以使用父尺寸缩放位图,以避免OutOfMemory
,请检查this question了解更多详细信息。
// Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
// imageView.setImageBitmap(bitmap);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filePath,bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);