0

我有一个应用程序,其中包含需要备份的用户生成的大量文件。所有内容都存储在 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!

提前感谢您提供的任何帮助!如果您需要任何其他信息,请告诉我。

4

1 回答 1

1

以下是我所做的详细信息。一开始我尝试和你一样,我实现了,BackupAgentHelper但我很快意识到备份和恢复文件是不够的,我认为对于非常轻的数据或用户偏好来说还可以。就我而言,我需要备份一个 SQLite 数据库和许多相关的图片。

这基本上是我设置的过程:

对于备份:

  1. 第一步:AsyncTask复制并收集不同的文件以备份在外部存储上的同一文件夹中。
  2. 第二步:再次AsyncTask将所有文件压缩在一起
  3. 第三步:再次AsyncTask将 zip 传输到 Dropbox(我想我以后也可以用 Google Drive 来做)

对于修复:

  1. 第一步:AsyncTask将 zip 从 Dropbox 传输到设备。
  2. 第二步:再AsyncTask解压所有文件
  3. 第三步:另一个AsyncTask将文件移动到它们应该在的位置。

这样我就可以确保一切都按时完成,并且我可以跟踪所发生的事情。使用BackupAgentHelper,您无法轻松了解何时以及如何完成。

PS:对于 Dropbox,我使用了 Core API,他们提供了一个很好的项目示例(“DBRoulette”)以及提供的库。

于 2014-09-27T01:20:24.457 回答