I am new to firebase storage. Just so I could learn it, I made a simple app that has a button and an ImageView
. When I click on the button, an image (from
These soloutions really did not solve my problem.
what have solved my problem are those lines:
in app module
implementation 'com.google.firebase:firebase-database:15.0.0'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.google.firebase:firebase-auth:15.0.0'
and this code for uploading and getting a download link
private void uploadImageToFirebase(Bitmap bmp) {
try {
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/news";
File dir = new File(file_path);
if (!dir.exists())
dir.mkdirs();
File file = new File(dir, "sketchpad" + "_" + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
Log.i("SECONDACTIVITY", "ADDED SUCCESSFULLY");
Uri file2 = Uri.fromFile(file);
//Now Upload
final StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference riversRef = storageRef.child(file.getName());
UploadTask uploadTask;
uploadTask = riversRef.putFile(file2);
progressDialog.show();
progressDialog.setCancelable(false);
uploadTask.addOnProgressListener(new OnProgressListener() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
progressDialog.incrementProgressBy((int) progress);
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Uri DownloadLink = taskSnapshot.getDownloadUrl();
String imgUrl = DownloadLink.toString();
FirebaseDatabase.getInstance().getReference().child("image_link").setValue(imgUrl);
}
});
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}