gpt4 book ai didi

java - 应用程序无响应且用户权限错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:22 26 4
gpt4 key购买 nike

我有两个问题:

  1. 单击注册按钮后,应用程序将停止响应,并且不会保存数据库。
  2. 使用选择按钮选择照片后,单击上传按钮时出现用户权限错误。我不知道为什么会发生这种情况,因为我已经在 list 中添加了所有必需的权限。

这是我的代码

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 + "%");
}
});
}
}
}

最佳答案

  • 确保您已将此权限添加到 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);

关于java - 应用程序无响应且用户权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51238144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com