這是你可以上傳圖片到火力地堡存儲好運;)
private void uploadFromUri(Uri fileUri) {
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
// [START get_child_ref]
// Get a reference to store file at photos/<FILENAME>.jpg
final StorageReference photoRef = storageRef.child("images")
.child(fileUri.getLastPathSegment());
// [END get_child_ref]
// Upload file to Firebase Storage
// [START_EXCLUDE]
showProgressDialog();
// [END_EXCLUDE]
Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
photoRef.putFile(fileUri)
.addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// Upload succeeded
Log.d(TAG, "uploadFromUri:onSuccess");
// Get the public download URL
mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
Log.d("The Download Url", mDownloadUrl.toString());
// [START_EXCLUDE]
hideProgressDialog();
// updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Upload failed
Log.w(TAG, "uploadFromUri:onFailure", exception);
mDownloadUrl = null;
// [START_EXCLUDE]
hideProgressDialog();
Toast.makeText(NewProduct.this, "Error: upload failed",
Toast.LENGTH_SHORT).show();
// updateUI(mAuth.getCurrentUser());
// [END_EXCLUDE]
}
});
}
// [END upload_from_uri]
有錯在代碼示例中。請參閱http://stackoverflow.com/questions/38278249/update-progress-bar-with-firebase-uploading –
謝謝弗蘭克 工作 –
不要感謝我。我是在文檔中編寫破碎的代碼示例的人。 ;-)感謝@qbix(通過提高他的答案)。 –