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
Change your storage rules in Firebase console
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Go to storage -> Rules tab
add this
// Anyone can read or write to the bucket, even non-users of your app.
// Because it is shared with Google App Engine, this will also make
// files uploaded via GAE public.
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
Just go to Storage - Rules. Enter the rule as following, replacing the bucket name with your own bucket name. You can find the bucket name by going to Storage - Files.
It should look something like this: myapplication-xxxx.appspot.com
That's all you need. You don't need to enable authentication, if you are doing it for testing purposes only. If you need authentication, you can add it later.
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<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
progressDialog.incrementProgressBy((int) progress);
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@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();
}
}
service firebase.storage {
match /b/fir-upload-a2fa6.appspot.com/o {
match /{allPaths=**} {
// Allow access by all users
allow read, write;
}
}
}
The issue is with your firebase storage rules. Change it to
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Make sure the if request.auth != null