1

後でテストにも使用するタブレットで、Android アプリケーションによって作成された zip ファイルがあります。Android タブレットと PC で開くことができます。つまり、破損していません。この .zip ファイルを Android テスト プロジェクトの /res/raw フォルダーに追加しました。ここで、junit テストケースの 1 つのこのファイルを Android デバイスにコピーしたいと思います。そのために、次のコードを使用します。

    boolean success = false;
    File appDirectory = MainActivity.getContext().getExternalFilesDir(null);
    File pictureSketch = new File(appDirectory, sketchName+".zip");
    if (!appDirectory.exists()) {
        assertTrue("App directory could not be created.",appDirectory.mkdirs());    
    }

    InputStream in = activity.getResources().openRawResource(raw.pictest);
    FileOutputStream out = new FileOutputStream(pictureSketch);
    byte[] buff = new byte[1024];
    int read = 0;

    try {
       while ((read = in.read(buff)) > 0) {
          out.write(buff, 0, read);
       }
    } finally {
         in.close();

         out.close();
         success = true;
    }       

    return success;

正しいファイルを含む zip ファイルが作成されますが、開くとエラー メッセージが表示されます: 不正な zip ファイルです。転送後に zip ファイルを開くことができるようにするには、何を変更すればよいですか?

4

1 に答える 1

0

私のコードが間違っていたことはわかりませんでしたが、同じ目的を果たす回避策を見つけました:

結果:

private void copyPicSketch() {  
    AssetManager assetManager = getInstrumentation().getContext().getResources().getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", "Failed to get asset file list.", e);
    }
    for(String filename : files) {
        if (filename.contains(sketchName)) {
            InputStream in = null;
            OutputStream out = null;
            try {
              in = assetManager.open(filename);
              File outFile = new File(activity.getExternalFilesDir(null), filename);
              out = new FileOutputStream(outFile);
              copyFile(in, out);
              in.close();
              in = null;
              out.flush();
              out.close();
              out = null;
            } catch(IOException e) {
                Log.e("tag", "Failed to copy asset file: " + filename, e);
            }               
        }
    }
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
于 2013-09-10T15:22:14.257 に答える