จัดการข้อผิดพลาดสำหรับ Cloud Storage บน Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บางครั้งสิ่งต่างๆ อาจไม่เป็นไปตามแผนและเกิดข้อผิดพลาดขึ้น
หากไม่แน่ใจ ให้ตรวจสอบข้อผิดพลาดที่ส่งคืนและดูว่าข้อความแสดงข้อผิดพลาดระบุว่าอย่างไร
โค้ดต่อไปนี้แสดงการติดตั้งใช้งานตัวแฮนเดิลข้อผิดพลาดที่กำหนดเองซึ่งตรวจสอบ
รหัสข้อผิดพลาดและข้อความแสดงข้อผิดพลาดที่ Cloud Storage ส่งคืน คุณเพิ่มตัวแฮนเดิลข้อผิดพลาดดังกล่าวลงในออบเจ็กต์ต่างๆ ที่ใช้ใน Cloud Storage API ได้ (เช่น UploadTask
และ FileDownloadTask
)
Kotlin
internal inner class MyFailureListener : OnFailureListener {
override fun onFailure(exception: Exception) {
val errorCode = (exception as StorageException).errorCode
val errorMessage = exception.message
// test the errorCode and errorMessage, and handle accordingly
}
}
Java
class MyFailureListener implements OnFailureListener {
@Override
public void onFailure(@NonNull Exception exception) {
int errorCode = ((StorageException) exception).getErrorCode();
String errorMessage = exception.getMessage();
// test the errorCode and errorMessage, and handle accordingly
}
}
หากคุณได้ตรวจสอบข้อความแสดงข้อผิดพลาดและมีCloud Storage Security Rulesที่อนุญาตให้ดำเนินการ แต่ยังคงแก้ไขข้อผิดพลาดไม่ได้ โปรดไปที่หน้าการสนับสนุนของเราและแจ้งให้เราทราบว่าเราจะช่วยคุณได้อย่างไร
จัดการข้อความแสดงข้อผิดพลาด
ข้อผิดพลาดอาจเกิดขึ้นได้จากหลายสาเหตุ เช่น ไม่มีไฟล์
ผู้ใช้ไม่มีสิทธิ์เข้าถึงไฟล์ที่ต้องการ หรือ
ผู้ใช้ยกเลิกการอัปโหลดไฟล์
หากต้องการวินิจฉัยปัญหาและจัดการข้อผิดพลาดอย่างถูกต้อง โปรดดูรายการข้อผิดพลาดทั้งหมดที่ไคลเอ็นต์ของเราจะแจ้งและวิธีที่ข้อผิดพลาดเหล่านี้อาจเกิดขึ้น รหัสข้อผิดพลาดในตารางนี้
ได้รับการกำหนดในคลาส StorageException
เป็นค่าคงที่จำนวนเต็ม
รหัส |
เหตุผล |
ERROR_UNKNOWN |
เกิดข้อผิดพลาดที่ไม่รู้จัก |
ERROR_OBJECT_NOT_FOUND |
ไม่มีออบเจ็กต์ในการอ้างอิงที่ระบุ |
ERROR_BUCKET_NOT_FOUND |
ไม่ได้กำหนดค่าที่เก็บข้อมูลสำหรับ Cloud Storage |
ERROR_PROJECT_NOT_FOUND |
ไม่ได้กำหนดค่าโปรเจ็กต์สำหรับ Cloud Storage |
ERROR_QUOTA_EXCEEDED |
คุณใช้โควต้าในที่เก็บข้อมูล Cloud Storage เกินแล้ว หากคุณใช้แพ็กเกจราคา Spark โปรดพิจารณาอัปเกรดเป็นแพ็กเกจราคา Blaze แบบจ่ายตามการใช้งาน หากคุณใช้แพ็กเกจการกำหนดราคา Blaze อยู่แล้ว โปรดติดต่อทีมสนับสนุนของ Firebase
สำคัญ: ตั้งแต่วันที่ 1 ตุลาคม 2025 เป็นต้นไป คุณต้องใช้แพ็กเกจการกำหนดราคา Blaze เพื่อใช้ Cloud Storage แม้จะเป็นที่เก็บข้อมูลเริ่มต้นก็ตาม |
ERROR_NOT_AUTHENTICATED |
ผู้ใช้ไม่ได้รับการตรวจสอบสิทธิ์ โปรดตรวจสอบสิทธิ์แล้วลองอีกครั้ง |
ERROR_NOT_AUTHORIZED |
ผู้ใช้ไม่ได้รับอนุญาตให้ดำเนินการที่ขอ โปรดตรวจสอบกฎเพื่อให้แน่ใจว่าถูกต้อง |
ERROR_RETRY_LIMIT_EXCEEDED |
การดำเนินการ (อัปโหลด ดาวน์โหลด ลบ ฯลฯ) ใช้เวลาเกินขีดจำกัดสูงสุด โปรดลองอีกครั้ง |
ERROR_INVALID_CHECKSUM |
ไฟล์ในไคลเอ็นต์ไม่ตรงกับผลรวมตรวจสอบของไฟล์ที่เซิร์ฟเวอร์ได้รับ โปรดลองอัปโหลดอีกครั้ง |
ERROR_CANCELED |
ผู้ใช้ยกเลิกการดำเนินการ |
นอกจากนี้ การพยายามเรียกใช้ getReferenceFromUrl()
ด้วย URL ที่ไม่ถูกต้อง
จะทําให้เกิดการส่ง IllegalArgumentException
อาร์กิวเมนต์ของ
เมธอดข้างต้นต้องอยู่ในรูปแบบ gs://bucket/object
หรือ
https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN>
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-22 UTC
[null,null,["อัปเดตล่าสุด 2025-08-22 UTC"],[],[],null,["\u003cbr /\u003e\n\nSometimes things don't go as planned and an error occurs.\n\nWhen in doubt, check the error returned and see what the error message says.\nThe following code shows a custom error handler implementation that inspects\nthe error code and error message returned by Cloud Storage. Such error\nhandlers can be added to various objects used in the Cloud Storage API (for\nexample, `UploadTask` and `FileDownloadTask`). \n\nKotlin \n\n```kotlin\ninternal inner class MyFailureListener : OnFailureListener {\n override fun onFailure(exception: Exception) {\n val errorCode = (exception as StorageException).errorCode\n val errorMessage = exception.message\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L492-L498\n```\n\nJava \n\n```java\nclass MyFailureListener implements OnFailureListener {\n @Override\n public void onFailure(@NonNull Exception exception) {\n int errorCode = ((StorageException) exception).getErrorCode();\n String errorMessage = exception.getMessage();\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L618-L625\n```\n\nIf you've checked the error message and have Cloud Storage Security Rules that allow your\naction, but are still struggling to fix the error, visit our\n[Support page](/support) and let us know how we can help.\n\nHandle Error Messages\n\nThere are a number of reasons why errors may occur, including the file\nnot existing, the user not having permission to access the desired file, or the\nuser cancelling the file upload.\n\nTo properly diagnose the issue and handle the error, here is a full list of all\nthe errors our client will raise, and how they can occur. Error codes in this\ntable are defined in the `StorageException` class as integer constants.\n\n| Code | Reason |\n|------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ERROR_UNKNOWN` | An unknown error occurred. |\n| `ERROR_OBJECT_NOT_FOUND` | No object exists at the specified reference. |\n| `ERROR_BUCKET_NOT_FOUND` | No bucket is configured for Cloud Storage |\n| `ERROR_PROJECT_NOT_FOUND` | No project is configured for Cloud Storage |\n| `ERROR_QUOTA_EXCEEDED` | Quota on your Cloud Storage bucket has been exceeded. If you're on the Spark pricing plan, consider upgrading to the [pay-as-you-go Blaze pricing plan](/pricing). If you're already on the Blaze pricing plan, reach out to Firebase Support. **Important** : Starting October 1, 2025, the [Blaze pricing plan will be *required* to use Cloud Storage](/docs/storage/faqs-storage-changes-announced-sept-2024), even default buckets. |\n| `ERROR_NOT_AUTHENTICATED` | User is unauthenticated, please authenticate and try again. |\n| `ERROR_NOT_AUTHORIZED` | User is not authorized to perform the requested action, check your rules to ensure they are correct. |\n| `ERROR_RETRY_LIMIT_EXCEEDED` | The maximum time limit on an operation (upload, download, delete, etc.) has been excceded. Try again. |\n| `ERROR_INVALID_CHECKSUM` | File on the client does not match the checksum of the file received by the server. Try uploading again. |\n| `ERROR_CANCELED` | User canceled the operation. |\n\nAdditionally, attempting to call `getReferenceFromUrl()` with an invalid URL\nwill result in an `IllegalArgumentException` from being thrown. The argument to\nthe above method must be of the form `gs://bucket/object` or\n`https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=\u003cTOKEN\u003e`"]]