是否用戶上傳通過應用程序圖像還是你? 如果是這樣,我可以建議這樣做:
有一個私人圖像按鈕。
中的onCreate()
imageButton = (ImageButton) findViewById(R.id.ibImageSelect);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, Gallery_Request);
}
});
的方法體外:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Gallery_Request && resultCode == RESULT_OK) {
imageUri = data.getData();
imageButton.setImageURI(imageUri);
}
}
在您點擊鏈接
StorageReference upload = storageReference.child("what you want");
Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
UploadTask filePath = upload.child(imageUri.getLastPathSegment()).putBytes(data);
filePath.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
filePath.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
DatabaseReference newPost = mref.push(); //this generates a random unique key
replace with your own if you want, since you already got your own structure
newPost.child("image").setValue(imageUri.getLastPathSegment());
//set image name so can use this to download the image
}
});
你可以在每個節點的 「身份」 的孩子數據,然後當下載圖像時,你循環遍歷所有節點,檢查狀態是否是你想要的。然後獲取圖像下載密鑰。
下載圖片:
遍歷節點和兒童,獲得的圖像網址,
String ref = "image folder in firebase storage/" + imgurl;
storageRef.child(ref).getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
// Use the bytes to display the image
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView iv = (ImageView) findViewByID(R.id...);
iv.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("error", exception.getMessage());
}
});
謝謝您的回答。但是,這個解決方案並不能阻止黑客猜測文件名的Bruteforce攻擊。您的解決方案不會將數據保護爲尚未授權的訪問。或者我錯了? – user3853134
編輯:數據庫的價格爲20 GB /月的價格大約是存儲的256倍,因此我會優先考慮使用存儲。 :) https://firebase.google.com/pricing/ – user3853134