Android Studio批量缩放图片工具推荐及大图片缩放内存问题咨询
Hey there, let's work through your Android image scaling headaches together—you’ve got a couple of key issues here: handling large image resizes without OOM errors, finding reliable batch tools in Android Studio, and fixing that over-sized export from Illustrator. Let’s break this down:
First, let’s clear up a common density ratio misunderstanding: Android’s density buckets follow this scale: ldpi:mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi = 0.75:1:1.5:2:3:4. The problem you’re facing with OOM likely comes from starting with an overly large base image and scaling down from it—here’s how to fix that:
- Pick the right base density: Most modern devices use xxhdpi or xxxhdpi, so use xxhdpi as your baseline (e.g., a full-screen xxhdpi image only needs to be 1080×1920, not a 4K-sized file). Scale other densities from this base instead of shrinking a massive original, which avoids loading huge bitmaps into memory all at once.
- Use high-quality interpolation: When scaling, opt for bicubic interpolation over linear—it preserves more detail and reduces jagged edges, and most tools let you select this option.
- Preprocess large images externally: If you must work with a huge source file, first shrink it to close to your target base size in tools like Photoshop or GIMP before importing it into Android tools. This cuts down on memory pressure drastically.
The Android Drawable Importer plugin is notoriously finicky with large images. Try these more stable alternatives:
- Vector Asset Studio (Top Recommendation!): Since you create assets in Illustrator, export your work as an SVG file, then head to Android Studio’s
File > New > Vector Assetand import the SVG as a VectorDrawable. You’ll never need to generate multiple density bitmaps again—the system scales the vector dynamically for every device, eliminating OOM risks and reducing APK size. This is the best option for icons, simple UI elements, and any asset that’s vector-friendly. - Drawable Resizer Plugin: This lightweight plugin is a more reliable replacement for the old Drawable Importer. It lets you batch select images, set target density scales, and even adjust memory usage settings (like limiting how many images it processes at once) to avoid OOM. Just right-click a drawable folder and select "Resize Drawables" to get started.
- Gradle Script for Custom Batch Processing: If you want full control, write a simple Gradle task using ImageMagick’s
convertcommand (install ImageMagick first) to scale images in the background, avoiding Android Studio’s memory limits. Here’s a sample script:
task batchResizeDrawables { def baseDir = file("src/main/res/drawable-xxhdpi") // Your baseline density folder def densityScales = [ "ldpi": 0.25, "mdpi": 0.333, "hdpi": 0.5, "xhdpi": 0.666, "xxxhdpi": 1.333 ] baseDir.eachFileMatch(~/.*\.(png|jpg|jpeg)/) { inputFile -> densityScales.each { density, scale -> def outputDir = file("src/main/res/drawable-${density}") outputDir.mkdirs() def outputFile = new File(outputDir, inputFile.name) // Use ImageMagick to scale with quality control exec { commandLine "convert", inputFile.path, "-resize", "${scale * 100}%", "-quality", "90", outputFile.path } } } }
Run it with ./gradlew batchResizeDrawables—it handles scaling in the background without eating up Studio’s memory.
Your current exports are too large because you’re not applying Android’s density scales during export:
- Set a baseline artboard: In Illustrator, create an artboard sized to your target base density (e.g., 1080×1920 for xxhdpi full-screen assets).
- Export with density-specific scaling: Go to
File > Export > Export As, select PNG, then adjust the scale percentage for each density:- ldpi: 25%
- mdpi: 33.3%
- hdpi: 50%
- xhdpi: 66.6%
- xxhdpi: 100% (your baseline)
- xxxhdpi: 133.3%
- Skip bitmaps entirely (best option): Export your Illustrator asset as an SVG (make sure to uncheck "Responsive" and set the correct artboard size), then import it into Android Studio as a VectorDrawable—no more scaling or OOM issues ever.
- Boost Studio’s memory allocation: Find the
studio.vmoptionsfile (or go toFile > Settings > Build, Execution, Deployment > Build Tools > Gradle) and increase the-Xmxparameter to-Xmx4096mor-Xmx8192mto give Studio more memory for processing large files. - Free up system resources: Close unnecessary apps like browsers or other IDEs before running batch scaling tools—every bit of free memory helps.
内容的提问来源于stack exchange,提问作者Saad Zahoor

