{"record":{"id":"0e162725b99faf4a","repo":"koral--/android-gif-drawable","slug":"sample-size-samplesize-out-of-range-1-65535","errorCode":null,"errorMessage":"Sample size <sampleSize> out of range <1, 65535>","messagePattern":"Sample size <sampleSize> out of range <1, 65535>","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifAnimationMetaData.java","lineNumber":246,"sourceCode":"\t *\n\t * @return possible size of the memory needed to store pixels excluding backing {@link android.graphics.Bitmap} and assuming no subsampling\n\t */\n\tpublic long getAllocationByteCount() {\n\t\treturn mPixelsBytesCount;\n\t}\n\n\t/**\n\t * Like {@link #getAllocationByteCount()} but includes also backing {@link android.graphics.Bitmap} and takes sample size into account.\n\t *\n\t * @param oldDrawable optional old drawable to be reused, pass {@code null} if there is no one\n\t * @param sampleSize  sample size, pass {@code 1} if not using subsampling\n\t * @return possible size of the memory needed to store pixels\n\t * @throws IllegalArgumentException if sample size out of range\n\t */\n\t@Beta\n\tpublic long getDrawableAllocationByteCount(@Nullable GifDrawable oldDrawable, @IntRange(from = 1, to = Character.MAX_VALUE) int sampleSize) {\n\t\tif (sampleSize < 1 || sampleSize > Character.MAX_VALUE) {\n\t\t\tthrow new IllegalStateException(\"Sample size \" + sampleSize + \" out of range <1, \" + Character.MAX_VALUE + \">\");\n\t\t}\n\n\t\tfinal int sampleSizeFactor = sampleSize * sampleSize;\n\t\tfinal long bufferSize;\n\t\tif (oldDrawable != null && !oldDrawable.mBuffer.isRecycled()) {\n      bufferSize = oldDrawable.mBuffer.getAllocationByteCount();\n    } else {\n\t\t\tbufferSize = (mWidth * mHeight * 4L) / sampleSizeFactor;\n\t\t}\n\t\treturn (mPixelsBytesCount / sampleSizeFactor) + bufferSize;\n\t}\n\n\t/**\n\t * See{@link GifDrawable#getMetadataAllocationByteCount()}\n\t *\n\t * @return maximum possible size of the allocated memory needed to store metadata\n\t */\n\tpublic long getMetadataAllocationByteCount() {","sourceCodeStart":228,"sourceCodeEnd":264,"githubUrl":"https://github.com/koral--/android-gif-drawable/blob/26ff795f78b7fe8bbaf375a947b2bda95fc58e3d/android-gif-drawable/src/main/java/pl/droidsonroids/gif/GifAnimationMetaData.java#L228-L264","documentation":"getDrawableAllocationByteCount estimates the memory a GifDrawable will need for its pixel buffer at a given sample size. The library validates that sampleSize is within <1, 65535> (Character.MAX_VALUE); anything outside that range cannot produce a valid downsampled bitmap, so it throws. Note the javadoc says IllegalArgumentException but the code actually throws IllegalStateException.","triggerScenarios":"Calling getDrawableAllocationByteCount(oldDrawable, sampleSize) with sampleSize < 1 or sampleSize > 65535, e.g. passing 0, a negative value from a failed calculation, or an unbounded user input.","commonSituations":"Computing sample size dynamically (e.g. from desired display size divided by intrinsic size) where a division yields 0 for very large desired sizes; user-configurable quality settings with no bounds checking; off-by-one when clamping.","solutions":["Clamp sampleSize before calling: Math.max(1, Math.min(sampleSize, Character.MAX_VALUE))","Validate the source of the sample size (settings, division result) and default to 1 on invalid input","Catch IllegalStateException as a safety net and retry with sampleSize 1"],"exampleFix":"// before\nlong bytes = metadata.getDrawableAllocationByteCount(null, requestedSampleSize);\n// after\nint sampleSize = Math.max(1, Math.min(requestedSampleSize, Character.MAX_VALUE));\nlong bytes = metadata.getDrawableAllocationByteCount(null, sampleSize);","handlingStrategy":"validation","validationCode":"if (sampleSize < 1 || sampleSize > Character.MAX_VALUE) { throw new IllegalArgumentException(\"sampleSize out of range: \" + sampleSize); }","typeGuard":"boolean isValidSampleSize(int s) { return s >= 1 && s <= Character.MAX_VALUE; }","tryCatchPattern":"try { bytes = meta.getDrawableAllocationByteCount(null, sampleSize); } catch (IllegalStateException e) { bytes = meta.getDrawableAllocationByteCount(null, 1); }","preventionTips":["Always clamp sampleSize to <1, 65535> before calling","Bound any user- or settings-supplied sample size at input time","Remember 1 means full resolution; prefer small defaults"],"tags":["android","gif","argument-validation"],"backgroundTag":"value-out-of-range","analyzedSha":"26ff795f78b7fe8bbaf375a947b2bda95fc58e3d","analyzedAt":"2026-09-10T14:37:11.595Z","contentChangedAt":"2026-09-10T14:37:11.595Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}