Reputation: 426
I have a problem when I upload an image to firebase.
When I Upload the image I get one taskSnapshot.getDownloadUrl() token, and when I look in firebase storage there is a diffrent token key. I can't figure out where my problem is, and it is not every time I get the error (about 30 % of the time).
Hope you can help me.
My upload method:
public void uploadImage(Context context, Activity activity) {
String authorities = activity.getPackageName() + ".fileprovider";
File f = new File(MainActivity.imagesPath + String.valueOf(uploadToiletId) + "_" + uploadTimeStamp + ".png");
Log.i("HANDLERTAG", "file: " + f.getPath());
Uri imageUri = FileProvider.getUriForFile(context, authorities, f);
Log.i("HANDLERTAG", "image: " + imageUri.getPath());
Bitmap bitmap;
ByteArrayOutputStream baos;
final DatabaseReference databaseRef = FirebaseDatabase.getInstance().getReference();
final StorageReference storageRef = FirebaseStorage.getInstance().getReference();
final FirebaseAuth mAuth = FirebaseAuth.getInstance();
try {
bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageUri);
Bitmap resized = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() / 4, bitmap.getHeight()/ 4, true);
baos = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.PNG, 50, baos);
byte[] dataBAOS = baos.toByteArray();
final String timeStamp = FormatHelper.getCurrentDateTimeString("dd-MM-yy HH:mm:ss");
StorageReference imagesRef = storageRef.child(String.valueOf(uploadToiletId)).child(timeStamp + ".png");
imagesRef.putBytes(dataBAOS).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.i("CAMERATAG", "Billede uploadet");
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.i("DOWNLOAD_URL",String.valueOf(downloadUrl));
HashMap<String, String> UploadImageMap = new HashMap<>();
UploadImageMap.put("Url", downloadUrl.toString());
UploadImageMap.put("UserId", mAuth.getCurrentUser().getUid());
databaseRef.child(DB_TABLE_TOILETS)
.child(String.valueOf(uploadToiletId))
.child("Images")
.child(timeStamp).setValue(UploadImageMap);
databaseRef.child(DB_TABLE_USERS)
.child(mAuth.getCurrentUser().getUid())
.child("Images")
.child(String.valueOf(uploadToiletId))
.child(timeStamp).setValue(UploadImageMap);
}
});
imagesRef.putBytes(dataBAOS).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("CAMERATAG", e.getLocalizedMessage());
Log.i("CAMERATAG", "Noget gik galt med billede");
}
});
Toast.makeText(context, "Upload complete", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.i("HANDLERTAG", e.getMessage());
e.printStackTrace();
}
}
Debug shows token key from my app in android studio
Token key: token=b23e8ab1-8322-46cf-889e-4f276fb2c242
Token key from firebase: token=e5b84bed-7226-4af3-b01b-e31cf03e7d8b
Upvotes: 1
Views: 671
Reputation: 1169
Have you tried setting your storage to read true?
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}
This way users can read without being logged in, but has to be logged in to actually write to your storage.
Upvotes: 3