0

我使用以下代码将目录和一些文件复制到 sdcard。我复制的目录放在 assets 文件夹中。

public class CopyAssets  extends Activity{
Button btn;
File sdCard = Environment.getExternalStorageDirectory();
private CameraBaseActivity mCamBase;
protected static String copyStatus;
CopyAssets(String path){
    //copyFileOrDir(path);
    copyStatus = "loading";
    mCamBase = CameraBaseActivity.getInstance();
    if(isSdPresent())
    new MoveDir().execute(path);
}

private class MoveDir extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        copyStatus = "loaded";
        copyFileOrDir("edu.cmu.pocketsphinx");
        return (copyStatus);
    }

    @Override
    protected void onPostExecute(String result) {
        //TextView txt = (TextView) findViewById(R.id.output);
        //txt.setText("Executed"); // txt.setText(result);
        // might want to change "executed" for the returned string passed
        // into onPostExecute() but that is upto you
        mCamBase.initARParams();
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

private void copyFileOrDir(String path) {
    AssetManager assetManager = this.getAssets();
    String assets[] = null;
    try {
        assets = assetManager.list(path);
        if (assets.length == 0) {
            copyFile(path);
        } else {
            File dir = new File(sdCard.getAbsolutePath() + "/" + path);
            if (!dir.exists()) {
                System.out.println("Created directory"
                        + sdCard.getAbsolutePath());
                boolean result = dir.mkdir();
                System.out.println("Result of directory creation" + result);
            }

            for (int i = 0; i < assets.length; ++i) {
                copyFileOrDir(path + "/" + assets[i]);
            }
        }
    } catch (IOException ex) {
        System.out.println("Exception in copyFileOrDir" + ex);
    }
}

private void copyFile(String filename) {
    AssetManager assetManager = this.getAssets();

    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(filename);
        // String newFileName = "/data/data/" + this.getPackageName() + "/"
        // + filename;//path for storing internally to data/data
        String newFileName = sdCard.getAbsolutePath() + "/" + filename;
        out = new FileOutputStream(newFileName);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        System.out.println("File created in path" + newFileName);
    } catch (Exception e) {
        System.out.println("Exception in copyFile" + e);
        // System.out.println("Exception in copyFile"+e.print);
        // Log.e("tag", e.printStackTrace());
    }

}
public static boolean isSdPresent() {
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}
}

例外是在doinbackground:

01-01 00:00:41.679: WARN/dalvikvm(1530): threadid=11: thread exiting with uncaught exception (group=0x40b3d1f8)
01-01 00:00:41.687: ERROR/CameraBaseActivity(1530): uncaughtException.
01-01 00:00:41.695: ERROR/CameraBaseActivity(1530):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-01 00:00:41.695: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-01 00:00:41.695: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-01 00:00:41.703: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-01 00:00:41.703: INFO/ActivityManager(292): Start proc com.test.android.imageplugin for service com.test.android.imageplugin/.ImagePluginServer: pid=1549 uid=6062 gids={1015, 1023}
01-01 00:00:41.710: ERROR/LL_Core(1530): [LifeLogSecurityUtility]lifelogdb permission error
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530):     at java.lang.Thread.run(Thread.java:856)
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530): java.lang.RuntimeException: An error occured while executing doInBackground()
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530): uncaughtException.
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530): uncaughtException : other thread.
01-01 00:00:41.710: ERROR/CameraBaseActivity(1530): LLDB regist error. unregisted.
01-01 00:00:41.710: DEBUG/Camera(1530): Capture get() mIsExecuteRegistMenu=false
01-01 00:00:41.710: DEBUG/Camera(1530): onResumeImpl start
01-01 00:00:41.710: DEBUG/Camera(1530): execDisplayLayoutChangeNonSync start rotation:0
01-01 00:00:41.726: DEBUG/Camera(1530): onResumeImpl end
01-01 00:00:41.726: INFO/CameraBaseActivity(1530): onResume end
01-01 00:00:41.726: INFO/CameraBaseActivity(1530): onPostResume
01-01 00:00:41.890: INFO/CameraBaseActivity(1530): Camera.finish

我找不到它在 doinbackground 中意外崩溃的原因,因为我在这里发布的代码看不到任何错误。非常感谢任何帮助。

4

2 回答 2

1

LifeLogSecurityUtility 是什么?

这条线是可能的:

[LifeLogSecurityUtility]lifelogdb permission error

告诉您清单中缺少权限标记。虽然我不熟悉这个 LifeLogSecurityUtility 所以我不知道那个东西希望你拥有哪个权限。

于 2012-06-20T15:45:49.180 回答
1

如果您还没有这样做,请在您的 AndroidManifest.xml 中添加 WRITE_EXTERNAL_STORAGE 权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-06-20T15:47:09.970 回答