我有一个应用程序,其中包含需要备份的用户生成的大量文件。所有内容都存储在 db、preferences 或 4 个文件夹中的 1 个中。我编写了一个简单的辅助函数来FileBackupHelpers
为每个文件夹中的文件生成所有文件。但是,似乎在我onCreate()
运行框架代码之后FileBackupHelper
,要解析的路径数量令人窒息,并使我的应用程序崩溃。
这是我的备份代理的代码:
package <packageNameRemovedToProtectClient>.service;
import android.app.backup.BackupAgentHelper;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import <packageNameRemovedToProtectClient>.data.DBManager;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;
/**
* Created by mclark4386 on 9/18/14.
*/
public class AABackupAgent extends BackupAgentHelper {
private static final String CLASS_NAME = AABackupAgent.class.getSimpleName();
private Context mContext;
public AABackupAgent() {
super();
}
@Override
public void onCreate() {
super.onCreate();
mContext = this;
AssetManager.setupSingleton(mContext);
Log.v(CLASS_NAME,"GO!");
this.addHelper("prefs", new SharedPreferencesBackupHelper(mContext, Service.PREFS_NAME));
String dbfile = DBManager.filePath.replace(".sqlite", "");
Log.v(CLASS_NAME,"DB file:"+dbfile);
File dataDirectory = Environment.getDataDirectory();
File db = new File(dataDirectory,DBManager.fullDBPath);
Log.v(CLASS_NAME,"path:"+db.getAbsolutePath());
this.addHelper("db", new FileBackupHelper(mContext, db.getAbsolutePath()));
AddFolderHelper(AssetManager.thingImageFolder.getAbsolutePath());
AddFolderHelper(AssetManager.userFolder.getAbsolutePath());
AddFolderHelper(AssetManager.categoryImageFolder.getAbsolutePath());
AddFolderHelper(AssetManager.audioFolder.getAbsolutePath());
Log.v(CLASS_NAME,"onCreate Done!");
}
private void AddFolderHelper(String path){
try{
File folder = new File(path);
if (folder.isDirectory()){
Log.v(CLASS_NAME,"Filebackup list size::"+folder.list().length+" for path:"+folder.getName());
File[] listFiles = folder.listFiles();
Vector<String> filepaths = new Vector<String>(listFiles.length);
for (int i = 0;i< listFiles.length;i++)
filepaths.add(listFiles[i].getAbsolutePath());
if (filepaths.size()>512){
int count = (filepaths.size()/511);
if(filepaths.size()%511 > 0)count++;
Log.v(CLASS_NAME,"should break up the paths in to "+count+" chunks!");
String[] paths = new String[filepaths.size()];
filepaths.toArray(paths);
String shortName = folder.getName();
for (int i = 0; i < count; i++) {
Log.v(CLASS_NAME,"adding helper for path:"+shortName+i);
String[] pathRange = Arrays.copyOfRange(paths, 511 * i, (511 * i) + 511);
this.addHelper(shortName+i, new FileBackupHelper(mContext, pathRange));
}
}else {
String[] paths = new String[filepaths.size()];
filepaths.toArray(paths);
this.addHelper(path, new FileBackupHelper(mContext, paths));
}
}else{
this.addHelper(path, new FileBackupHelper(mContext, path));
}
}catch (Exception e){
Log.e(CLASS_NAME,"ERROR: AddFolderHelper:");
e.printStackTrace();
}
}
}
这是日志输出:
V/AABackupAgent﹕ GO!
V/AABackupAgent﹕ DB file:AADataModel
V/AABackupAgent﹕ path:/data/data/<packageNameRemovedToProtectClient>/databases/AADataModel.sqlite
V/AABackupAgent﹕ Filebackup list size::551 for path:thingImage
V/AABackupAgent﹕ should break up the paths in to 2 chunks!
V/AABackupAgent﹕ adding helper for path:thingImage0
V/AABackupAgent﹕ adding helper for path:thingImage1
V/AABackupAgent﹕ Filebackup list size::1 for path:user
V/AABackupAgent﹕ Filebackup list size::0 for path:categoryImage
V/AABackupAgent﹕ Filebackup list size::551 for path:audio
V/AABackupAgent﹕ should break up the paths in to 2 chunks!
V/AABackupAgent﹕ adding helper for path:audio0
V/AABackupAgent﹕ adding helper for path:audio1
V/AABackupAgent﹕ onCreate Done!
V/BackupServiceBinder﹕ doBackup() invoked
D/BackupHelperDispatcher﹕ handling existing helper 'db' android.app.backup.FileBackupHelper@4336b8e8
D/BackupHelperDispatcher﹕ handling existing helper 'prefs' android.app.backup.SharedPreferencesBackupHelper@43595ce8
D/BackupHelperDispatcher﹕ handling new helper '/storage/emulated/0/Android/data/<packageNameRemovedToProtectClient>/files/categoryImage'
D/BackupHelperDispatcher﹕ handling new helper '/storage/emulated/0/Android/data/<packageNameRemovedToProtectClient>/files/user'
D/BackupHelperDispatcher﹕ handling new helper 'audio0'
E/dalvikvm﹕ JNI ERROR (app bug): local reference table overflow (max=512)
W/dalvikvm﹕ JNI local reference table (0x7e2ff770) dump:
W/dalvikvm﹕ Last 10 entries (of 512):
W/dalvikvm﹕ 511: 0x426c0878 java.lang.String "/data/data/com.m... (132 chars)
W/dalvikvm﹕ 510: 0x426c0830 java.lang.String "/data/data/com.m... (138 chars)
W/dalvikvm﹕ 509: 0x426c0500 java.lang.String "/data/data/com.m... (127 chars)
W/dalvikvm﹕ 508: 0x426c01d8 java.lang.String "/data/data/com.m... (131 chars)
W/dalvikvm﹕ 507: 0x426ba7d8 java.lang.String "/data/data/com.m... (131 chars)
W/dalvikvm﹕ 506: 0x426ba790 java.lang.String "/data/data/com.m... (137 chars)
W/dalvikvm﹕ 505: 0x426bf488 java.lang.String "/data/data/com.m... (135 chars)
W/dalvikvm﹕ 504: 0x426bf158 java.lang.String "/data/data/com.m... (130 chars)
W/dalvikvm﹕ 503: 0x426bee38 java.lang.String "/data/data/com.m... (129 chars)
W/dalvikvm﹕ 502: 0x426b94b0 java.lang.String "/data/data/com.m... (131 chars)
W/dalvikvm﹕ Summary:
W/dalvikvm﹕ 1 of java.lang.Class
W/dalvikvm﹕ 507 of java.lang.String (507 unique instances)
W/dalvikvm﹕ 2 of java.io.FileDescriptor (2 unique instances)
W/dalvikvm﹕ 2 of java.lang.String[] (511 elements) (2 unique instances)
E/dalvikvm﹕ Failed adding to JNI local ref table (has 512 entries)
I/dalvikvm﹕ "Binder_3" prio=5 tid=20 RUNNABLE
I/dalvikvm﹕ | group="main" sCount=0 dsCount=0 obj=0x42f4b218 self=0x777b2a98
I/dalvikvm﹕ | sysTid=10236 nice=0 sched=0/0 cgrp=apps handle=1942542136
I/dalvikvm﹕ | state=R schedstat=( 18035885 5737305 43 ) utm=1 stm=0 core=3
I/dalvikvm﹕ at android.app.backup.FileBackupHelperBase.performBackup_native(Native Method)
I/dalvikvm﹕ at android.app.backup.FileBackupHelperBase.performBackup_checked(FileBackupHelperBase.java:76)
I/dalvikvm﹕ at android.app.backup.FileBackupHelper.performBackup(FileBackupHelper.java:85)
I/dalvikvm﹕ at android.app.backup.BackupHelperDispatcher.doOneBackup(BackupHelperDispatcher.java:95)
I/dalvikvm﹕ at android.app.backup.BackupHelperDispatcher.performBackup(BackupHelperDispatcher.java:76)
I/dalvikvm﹕ at android.app.backup.BackupAgentHelper.onBackup(BackupAgentHelper.java:66)
I/dalvikvm﹕ at android.app.backup.BackupAgent$BackupServiceBinder.doBackup(BackupAgent.java:567)
I/dalvikvm﹕ at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:80)
I/dalvikvm﹕ at android.os.Binder.execTransact(Binder.java:404)
I/dalvikvm﹕ at dalvik.system.NativeStart.run(Native Method)
I/dalvikvm﹕ [ 09-20 04:06:45.777 9815:10236 E/dalvikvm ] VM aborting
A/libc﹕ Fatal signal 6 (SIGABRT) at 0x00002657 (code=-6), thread 10236 (Binder_3)
W/ContextImpl﹕ Failed to ensure directory: /storage/extSdCard/Android/data/<packageNameRemovedToProtectClient>/files
W/ContextImpl﹕ Failed to ensure directory: /storage/extSdCard/Android/data/<packageNameRemovedToProtectClient>/files
V/AABackupAgent﹕ GO!
V/AABackupAgent﹕ DB file:AADataModel
V/AABackupAgent﹕ path:/data/data/<packageNameRemovedToProtectClient>/databases/AADataModel.sqlite
V/AABackupAgent﹕ Filebackup list size::551 for path:thingImage
V/AABackupAgent﹕ should break up the paths in to 2 chunks!
V/AABackupAgent﹕ adding helper for path:thingImage0
V/AABackupAgent﹕ adding helper for path:thingImage1
V/AABackupAgent﹕ Filebackup list size::1 for path:user
V/AABackupAgent﹕ Filebackup list size::0 for path:categoryImage
V/AABackupAgent﹕ Filebackup list size::551 for path:audio
V/AABackupAgent﹕ should break up the paths in to 2 chunks!
V/AABackupAgent﹕ adding helper for path:audio0
V/AABackupAgent﹕ adding helper for path:audio1
V/AABackupAgent﹕ onCreate Done!
提前感谢您提供的任何帮助!如果您需要任何其他信息,请告诉我。