3

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();
                    }
                });
    }
}
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
jujisan
  • 39
  • 4
  • Could be a runtime permission issue. Starting from API 23, you need runtime permission to access gallert to pick images. https://stackoverflow.com/questions/39866869/how-to-ask-permission-to-access-gallery-on-android-m/39866945 – Redwanul Haque Sourave May 01 '20 at 19:34
  • You cannot get download url like this => taskSnapshot.getStorage().getDownloadUrl().toString() – Kasım Özdemir May 01 '20 at 19:47
  • This doesn't work: `taskSnapshot.getStorage().getDownloadUrl().toString()` The correct way to get the download URL, see the [documentation](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) or https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions – Frank van Puffelen May 01 '20 at 19:57
  • Try uploading with a `VPN`. You can use `Hotspot Sheild VPN`. If uploading works with VPN, then you've to understand that there is a problem with your country's internet service. Because, In `Bangladesh` firebase storage's native sdk not working from May 29, 2020. It's an issue with Bangladesh's `BTCL`. We can nothing to do with it. So, give a try with Hotspot Sheild VPN. – Shariful Islam Mubin May 02 '20 at 06:31
  • 1
    **mubin986** I used SuperVPN in this regard. But still nothing happened – jujisan May 02 '20 at 08:05

1 Answers1

0

please can u tell me if the imageuri is not null? try to print imageuri in log to check if it is not null.

MueEZ
  • 136
  • 1
  • 1
  • 7