Это моя UploadActivity. Приложение позволяет мне выбрать файл, но вылетает, как только я нажимаю кнопку загрузки. Toast сообщение OnFailedListener отображается каждый раз. Я выбираю файл correctimage с правильными соглашениями об именах, но все еще сталкиваюсь с той же проблемой. Я сослался на документацию, а также на учебники и реализовал почти те же методы. Что я делаю неправильно?
public class UploadActivity extends AppCompatActivity {
Button selectfile,upload;
TextView notify;
Uri imguri;
ProgressDialog progressDialog;
FirebaseStorage storage;
FirebaseDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
storage = FirebaseStorage.getInstance();
database = FirebaseDatabase.getInstance();
selectfile = findViewById(R.id.SelectFile);
upload = findViewById(R.id.UploadFile);
notify = findViewById(R.id.notify);
selectfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ContextCompat.checkSelfPermission(UploadActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
selectPDF();
}
else{
ActivityCompat.requestPermissions(UploadActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},9);
}
}
});
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(imguri!=null){
uploadFile(imguri);
}
else{
Toast.makeText(UploadActivity.this, "Please Select a File", Toast.LENGTH_SHORT).show();
}
}
});
}
private void uploadFile(Uri imguri) {
final String filename = System.currentTimeMillis()+"";
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("Uploading File... ");
progressDialog.setProgress(0);
progressDialog.show();
StorageReference storageReference = storage.getReference();
storageReference.child("Uploads").child(filename).putFile(imguri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> task = taskSnapshot.getMetadata().getReference().getDownloadUrl();
task.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
DatabaseReference databaseReference = database.getReference();
databaseReference.child(filename).setValue(url).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(UploadActivity.this, "Successfully Uploaded", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(UploadActivity.this, "Please Try Again", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(UploadActivity.this, "File was not uploaded, please try again!", Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
int currentProgress = (int) (100*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
progressDialog.setProgress(currentProgress);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode==9 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
selectPDF();
}
else{
Toast.makeText(this, "Please Provide External Storage Permission", Toast.LENGTH_LONG).show();
}
}
private void selectPDF() {
Intent intent = new Intent();
intent.setType("images/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,86);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 86 && resultCode == RESULT_OK && data != null) {
imguri = data.getData();
notify.setText("File Selected : "+ data.getData().getLastPathSegment());
} else {
Toast.makeText(this, "Please Select a file", Toast.LENGTH_SHORT).show();
}
}
}