0

次のコードを使用して、ディレクトリといくつかのファイルを SD カードにコピーします。コピーしたディレクトリは、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

まだ行っていない場合は、WRITE_EXTERNAL_STORAGE 権限を AndroidManifest.xml に追加します。

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