Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDownloadUrl not working and no idea how to solve it #76

Open
JuliusWildenburg opened this issue Nov 24, 2019 · 2 comments
Open

getDownloadUrl not working and no idea how to solve it #76

JuliusWildenburg opened this issue Nov 24, 2019 · 2 comments

Comments

@JuliusWildenburg
Copy link

`public class Upload_Images_Activity extends AppCompatActivity {

private static final int PICK_IMAGE_REQUEST=1;

private Button mButtonChooseImage;
private Button mButtonUpload;
private TextView mTextViewShowUploads;
private EditText mEditTextFileName;
private ImageView mImageView;
private ProgressBar mProgressbar;
private String MyUID, photoStringLink;
private TextView tvImageUri;
private Uri mImageUri;

private StorageReference mStorageRef;
private DatabaseReference mDatabaseRef;

private StorageTask mUploadTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upload_images);

    mButtonChooseImage = findViewById(R.id.btnChooseImage);
    mButtonUpload = findViewById(R.id.btnUploadImage);
   // mTextViewShowUploads = findViewById(R.id.tvShowUploads);
    mEditTextFileName = findViewById(R.id.etImageName);
    mImageView = findViewById(R.id.ivUploadedImage);
    mProgressbar = findViewById(R.id.pbUploadingImage);
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    MyUID = user.getUid();
    mStorageRef = FirebaseStorage.getInstance().getReference(MyUID).child("General_Image_Posts");
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("General_Image_Posts").child(MyUID);
    mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openFileChooser();
        }
    });

    mButtonUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mUploadTask != null && mUploadTask.isInProgress()){
                Toast.makeText(Upload_Images_Activity.this, "Upload in progress", Toast.LENGTH_SHORT).show();
            }else
            uploadFile();
        }
    });

  /*  mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openImagesActivity();
        }
    });*/



}

private void openFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, PICK_IMAGE_REQUEST);
}

@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) {
        mImageUri = data.getData();
        Picasso.get().load(mImageUri).fit().centerCrop().into(mImageView);
    }
}


private String getFileExtension(Uri uri) { //om extension van bestand te krijgen
    ContentResolver cR = getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cR.getType(uri));
}

private void uploadFile() {
    if (mImageUri != null) {
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()+ "." + getFileExtension(mImageUri));
        mUploadTask = fileReference.putFile(mImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mProgressbar.setProgress(0);
                            }
                        },500);
                        mImageView.setImageResource(0);
                        mImageUri = null;


                        Toast.makeText(Upload_Images_Activity.this, "Upload succesful", Toast.LENGTH_SHORT).show();
                        Upload upload = new Upload(mEditTextFileName.getText().toString().trim(),
                                taskSnapshot.**getDownloadUrl()**.toString());
                        String uploadId = mDatabaseRef.push().getKey();
                        mDatabaseRef.child(uploadId).setValue(upload);
                        mEditTextFileName.getText().clear();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(Upload_Images_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(@NonNull UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0* taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                        mProgressbar.setProgress((int) progress);
                    }
                });}
    else {
        Toast.makeText(this, "No file selected", Toast.LENGTH_LONG).show();
    }
}

}
`

@jonathanbcsouza
Copy link

Hey @JuliusWildenburg this gist might help you:
https://gist.github.com/jonathanbcsouza/13929ab81077645f1033bf9ce45beaab

@BarshaPal
Copy link

BarshaPal commented Aug 4, 2021

               String downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                
                FriendlyMessage friendlyMessage = new FriendlyMessage(null, mUsername, downloadUrl);

TRY,THIS MAY WORK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants