上传图片时出现Firebase存储错误

时间:2020-03-24 11:30:44

标签: java android firebase firebase-storage

这是我的注册课程,我正在尝试上传个人资料图片

Signup.java

public class Signup extends AppCompatActivity {
EditText  Fullname, phone, emailid, password;
Button  Signupbtn,chooseimg;
TextView loginbtn;
EditText Imgname;
ImageView imgdisplay;
FirebaseAuth fAuth;
ProgressBar progressBar;
UserDB userdb;
DatabaseReference reff1;
StorageReference streff;
private Uri imguri;
private static final int PICK_IMAGE_REQUEST=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
     Fullname=(EditText)findViewById(R.id.FullName );
     phone=(EditText)findViewById(R.id.Phone );
    emailid=(EditText)findViewById(R.id.EmailID );
     password=(EditText)findViewById(R.id.Password );
    chooseimg=(Button) findViewById(R.id.uploadimgbtn );
    imgdisplay=(ImageView) findViewById(R.id.imagedisplay );
    Imgname=(EditText)findViewById(R.id.imgname );
    Signupbtn=(Button)findViewById(R.id.Signupbtn);
     loginbtn=(TextView)findViewById(R.id.TextNavLgn) ;
     fAuth=FirebaseAuth.getInstance();
     progressBar=findViewById(R.id.progressBar);
    reff1= FirebaseDatabase.getInstance().getReference().child("UserDB");
    streff= FirebaseStorage.getInstance().getReference("UserDB");
     chooseimg.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             openfilechooser();
         }
     });

    userdb=new UserDB();


    if(fAuth.getCurrentUser()!=null){
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
               finish();
    }
    loginbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            OpenLoginPage();
        }
    });
     Signupbtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             onsignupbtn();

         }



     });
}
public void OpenLoginPage(){
    Intent intent1 =new Intent(getApplicationContext(),Login.class);
    startActivity(intent1);
}
public void openfilechooser(){
    Intent pfc=new Intent();
    pfc.setType("image/*");
    pfc.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(pfc,PICK_IMAGE_REQUEST);


}
public void onsignupbtn(){
    Upload();

    String EmailID=emailid.getText().toString().trim();
    String Password=password.getText().toString().trim();
    String fname=Fullname.getText().toString().trim();
    String number=phone.getText().toString().trim();

    userdb.setFullName(Fullname.getText().toString().trim());
    userdb.setNumber(phone.getText().toString().trim());
    userdb.setEmail(emailid.getText().toString().trim());
    reff1.push().setValue(userdb);


    if(TextUtils.isEmpty(EmailID)){
        emailid.setError("Userid is required");
        return;
    }
    if(TextUtils.isEmpty(Password)){
        password.setError("password is required");
        return;
    }

    if(TextUtils.isEmpty(fname)){
        Fullname.setError("Fullanme  is required");
        return;
    }
    if(password.length()<8){
        password.setError("password must be 8 charcters long");
        return;

    }


    fAuth.createUserWithEmailAndPassword(EmailID,Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if(task.isSuccessful()){
                Toast.makeText(Signup.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getApplicationContext(),Login.class));
            }
            else{
                Toast.makeText(Signup.this, "Error Signing Up" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        }
    });

}


private String getExtension(Uri uri){
    ContentResolver cr=getContentResolver();
    MimeTypeMap mime=MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cr.getType(uri));
}
public void Upload(){
    if(imguri!=null){
        StorageReference filerefernce=streff.child(System.currentTimeMillis()
                +"."+getExtension(imguri));
        filerefernce.putFile(imguri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Handler handler= new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                progressBar.setProgress(0);
                            }
                        },500);
                        Toast.makeText(getApplicationContext(),"Upload Successfull",Toast.LENGTH_LONG).show();
                        streff.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                final  Uri downloadUrl=uri;
                            }
                        });

                        String Imgfilename=Imgname.getText().toString().trim();
                        String uploadID=reff1.push().getKey();
                        reff1.child(uploadID).setValue(Imgfilename);




                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(getApplicationContext(),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() );
                        progressBar.setProgress((int) progress);
                    }
                });
    }
    else {

        Toast.makeText(getApplicationContext(),"No Image selected",Toast.LENGTH_SHORT).show();

    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==PICK_IMAGE_REQUEST && resultCode== RESULT_OK
    && data !=null && data.getData() !=null){
        imguri=data.getData();
        Picasso.get().load(imguri).into(imgdisplay);
    }

}

我遇到了错误:

2020-03-24 16:50:53.597 19409-19941 / com.example.myapplication E / StorageException:发生StorageException。 发生未知错误,请检查HTTP结果代码和服务器响应的内部异常。 代码:-13000 HttpResult:0

0 个答案:

没有答案