Eclipse 中 res/drawable 目錄下圖片雙擊打開顯示亂碼/二進制內容,是因為系統/IDE 文件關聯錯誤,將 .png 關聯到文本編輯器而非圖片查看器。
根因分析
- Eclipse 內部編輯器嘗試以文本模式打開二進制圖片文件
- Windows 系統默認圖片查看器關聯被修改(如安裝第三方編輯器後)
- Eclipse 文件關聯配置
Window → Preferences → General → Editors → File Associations 錯誤
解決方案(按推薦順序)
方法一:修復 Windows 系統圖片關聯(最徹底)
- 任意資源管理器中右鍵一張
.png 圖片
- 選擇 「打開方式」→「選擇其他應用」
- 選擇 「照片」 或 「Windows 照片查看器」
- 勾選「始終使用此應用打開 .png 文件」
- 重啟 Eclipse
方法二:配置 Eclipse 內部文件關聯
Window → Preferences → General → Editors → File Associations
- 文件類型添加
*.png、*.jpg、*.jpeg、*.gif、*.bmp、*.webp
- 關聯編輯器選擇 「System Default」 或 「Image Editor」 (若有)
- 點擊 Default 設置為默認
- Apply and Close,重啟 Eclipse
方法三:安裝 Eclipse 圖片預覽插件
- Help → Eclipse Marketplace → 搜索
Image Preview 或 Quick Image Viewer
- 安裝後,項目瀏覽器懸停/選中圖片即可預覽,雙擊用外部查看器
驗證修復
- Eclipse Package Explorer 中雙擊
drawable/ic_launcher.png
2. 應彈出系統圖片查看器,而非 Eclipse 文本編輯器
3. 或在 Eclipse 內部編輯器區域顯示圖片縮略圖 (取決於插件)
預防建議
- 安裝第三方編輯器 (VS Code, Notepad++, Sublime) 時,不要勾選「關聯所有文本文件」 或「關聯圖片文件」
- 定期檢查
File Associations 配置
- 專案中圖片命名規範:
ic_xxx.png, bg_xxx.jpg, img_xxx.webp,便於識別
現代替代:遷移到 Android Studio
Eclipse ADT 已於 2015 年停止更新。Android Studio 提供:
- 原生圖片預覽 (Project 視圖縮略圖、編輯器預覽)
- Vector Asset / Image Asset 向導
- WebP 無損轉換、自適應圖標生成
- Gradle 構建、Instant Run、Layout Inspector、Profiler
建議新專案直接使用 Android Studio,存量專案漸進式遷移(導入 Gradle、模組化)。