Last active
August 7, 2020 06:46
-
-
Save crazy-diya/7e1d9cd4396434ce20855c4a70f8acd5 to your computer and use it in GitHub Desktop.
save a image to firebase and get dowload link in Android Studio Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**Start the image save to Firebase Storage in the database*/ | |
private String getExtension(Uri uri){ | |
ContentResolver contentResolver = getContentResolver(); //using for get the extention in image. | |
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); | |
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri)); | |
} | |
private void FileUploader() { | |
storageTask = storageReference.child(System.currentTimeMillis()+"."+getExtension(imguri)).putFile(imguri) | |
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { | |
@Override | |
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { | |
progressBar.setVisibility(View.INVISIBLE); | |
/*Database ekata Image eke url eka save karawanawa*/ | |
HashMap<String,String> hashMap = new HashMap<>(); | |
//Get image Download URL// | |
Task<Uri> uriTask = taskSnapshot.getStorage().getDownloadUrl(); | |
while (!uriTask.isSuccessful()); | |
Uri downloadUrl = uriTask.getResult(); | |
hashMap.put("imgUrl",downloadUrl.toString()); | |
/*Upload upload = new Upload(downloadUrl.toString()); *//**Upload class eka yodaagena unath apita puluwan data tika store karaganna ee unath hash map ekak nikan paawichchi kala*//* | |
*/ | |
String imgKey = databaseReference.push().getKey(); | |
databaseReference.child(imgKey).setValue(/*upload*/hashMap); | |
Toast.makeText(ImageStore.this, "Image Uploaded Successfully!", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
/**End the image save to Firebase Storage in the database*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment