在Adobe Animate 2020中用AS3开发Android应用的运行时读写权限问题
Hey there, let's break down why your file-saving code isn't working on Android—even after adding permissions to the manifest. This is a super common gotcha with AS3 on Android, especially since newer Android versions changed how permissions and file access work.
First, Let's Diagnose the Core Issues
Static Manifest Permissions Aren't Enough (Android 6.0+)
Android 6.0 (API Level 23) and above require runtime permission requests. Adding permissions to your manifest is necessary, but you also need to explicitly ask the user for permission while the app is running—the system won't grant it automatically anymore.Missing Error Handling
Your current code doesn't listen for IO errors, so you're flying blind on why the save is failing. Always add anIOErrorEvent.IO_ERRORlistener to catch specific issues like permission denials or invalid file paths.File Path Considerations
File.documentsDirectoryon Android maps to a location that might have restricted access, especially in newer OS versions. For external storage, you'll want to use paths that align with Android's storage rules, like the app's dedicated external folder or the user's Documents directory.
Step-by-Step Fixes
1. Double-Check Your Manifest Permissions
Make sure your AndroidManifest.xml includes these permissions (add them inside the <manifest> tag):
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- For Android 11+, if you need broad external access, add this (use sparingly): --> <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
2. Add Runtime Permission Request & Error Handling
Modify your code to request permissions before attempting to save the file, and add error listeners to debug issues. Here's the updated version:
package { import flash.display.MovieClip; import flash.events.*; import flash.filesystem.*; import flash.system.Security; import flash.system.Capabilities; import flash.events.PermissionEvent; public class Writing extends MovieClip { var file: File; var str: String = "Hello."; private const WRITE_PERMISSION: String = "android.permission.WRITE_EXTERNAL_STORAGE"; public function Writing() { // Check if we're on Android 6.0+ and need to request permission if (Capabilities.os.indexOf("Android") != -1 && int(Capabilities.androidVersion) >= 23) { if (Security.sandboxType == "application" && Security.hasPermission(WRITE_PERMISSION)) { // Permission already granted, proceed to save initFileSave(); } else { // Request permission from the user Security.requestPermission(WRITE_PERMISSION); Security.addEventListener(PermissionEvent.PERMISSION_STATUS, onPermissionStatus); } } else { // Older Android or non-Android device, skip permission check initFileSave(); } } private function onPermissionStatus(event: PermissionEvent): void { Security.removeEventListener(PermissionEvent.PERMISSION_STATUS, onPermissionStatus); if (event.granted) { initFileSave(); } else { trace("Permission denied! Can't save file."); // Show a user-friendly message explaining why permission is needed } } private function initFileSave(): void { // Use a compliant external storage path (adjust as needed) // This saves to the user's Documents folder file = File.externalStorageDirectory.resolvePath("Documents/test.txt"); file.addEventListener(Event.COMPLETE, fileSaved); file.addEventListener(IOErrorEvent.IO_ERROR, onIOError); file.save(str); } function fileSaved(event: Event): void { trace("File saved successfully!"); } private function onIOError(event: IOErrorEvent): void { trace("File save error: " + event.text); // This will tell you exactly what went wrong (permission, path, etc.) } } }
3. Handle Scoped Storage (Android 10+)
If you're targeting Android 10 (API 29) or higher, scoped storage restricts direct access to the external storage root. For better compliance, save files to your app's dedicated external directory:
// Path: Android/data/your.package.name/files/test.txt file = File.applicationStorageDirectory.resolvePath("test.txt");
This path doesn't require special permissions beyond the basic write access we already requested.
Final Tips
- Test on a physical Android device—emulators often have permission quirks that don't reflect real-world behavior.
- Check Android Studio's logcat for more detailed error messages if the IOError listener doesn't give you enough info.
- For Android 11+, if you need to save files outside your app's directory, you'll need the
MANAGE_EXTERNAL_STORAGEpermission and guide users to enable it in system settings (it's a special, restricted permission).
内容的提问来源于stack exchange,提问作者Erick Gordillo

