将图片上传到firebase时出现问题。
当我上传图片时,我得到一个taskSnapshot.getDownloadUrl()令牌,当我查看firebase存储时,会有一个不同的令牌密钥。我无法弄清楚我的问题在哪里,而且每次我都没有得到错误(大约30%的时间)。
希望你能帮助我。我的上传方式:
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
令牌密钥:令牌= b23e8ab1-8322-46cf-889e-4f276fb2c242
来自firebase的令牌密钥:token = e5b84bed-7226-4af3-b01b-e31cf03e7d8b
答案 0 :(得分:3)
您是否尝试将存储设置为真实?
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}
这样用户无需登录即可阅读,但必须登录才能真正写入您的存储空间。