An image can be chosen by clicking the 'CHOOSE IMAGE' button and it will be shown in an imageview and will be uploaded to firebase storage after clicking 'SAVE IMAGE'.If it succeeds then data about that image will be uploaded to realtime firebase database. But nothing happens after clicking the 'SAVE IMAGE' button. What is the problem there?
These are my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button chooseButton,saveButton,displayButton;
private EditText imagenameEdittext;
private ImageView imageView;
private ProgressBar progressBar;
private Uri imageUri;
private StorageReference storageReference;
private DatabaseReference databaseReference;
private StorageTask uploadTask;
private static final int IMAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseReference = FirebaseDatabase.getInstance().getReference("Upload");
storageReference = FirebaseStorage.getInstance().getReference("Upload");
chooseButton = findViewById(R.id.chooseImageButtonid);
saveButton = findViewById(R.id.saveImageButtonid);
displayButton = findViewById(R.id.displayImageButtonid);
progressBar = findViewById(R.id.progressbarid);
imageView = findViewById(R.id.imageviewid);
imagenameEdittext = findViewById(R.id.imagenameEdittextid);
chooseButton.setOnClickListener(this);
saveButton.setOnClickListener(this);
displayButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.chooseImageButtonid:
openFileChooser();
break;
case R.id.saveImageButtonid:
if(uploadTask!=null && uploadTask.isInProgress()){
Toast.makeText(this, "Uploading in progress", Toast.LENGTH_SHORT).show();
}
else{
saveData();
}
saveData();
break;
case R.id.displayImageButtonid:
break;
}
}
public void openFileChooser(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==IMAGE_REQUEST && resultCode==RESULT_OK && data!=null && data.getData()!=null){
imageUri = data.getData();
Picasso.get().load(imageUri).into(imageView);
}
}
public String getFileExtension(Uri imageUri){
ContentResolver contentResolver = getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(imageUri));
}
private void saveData(){
final String imageName = imagenameEdittext.getText().toString().trim();
if(imageName.isEmpty()){
imagenameEdittext.setError("Enter the image name");
imagenameEdittext.requestFocus();
return;
}
StorageReference ref = storageReference.child(System.currentTimeMillis()+"."+getFileExtension(imageUri));
ref.putFile(imageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Get a URL to the uploaded content
Toast.makeText(MainActivity.this, "Image is stored Successfully", Toast.LENGTH_SHORT).show();
Upload upload = new Upload(imageName,taskSnapshot.getStorage().getDownloadUrl().toString());
String uploadId = databaseReference.push().getKey();
databaseReference.child(uploadId).setValue(upload);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(MainActivity.this, "Image store failed", Toast.LENGTH_SHORT).show();
}
});
}
}