본문 바로가기

IT 연구회

용량이 1MB가 넘는 파일을 asset에 담을 때

반응형
  • Android에서 raw 혹은 asset 리소스에 담을 수 있는 파일 크기는 최대 1MB(1024KB)로 제한되어 있다.
  • 만일 1MB가 넘는 파일을 꼭 리소스에 담고자 한다면, 파일을 1MB 단위로 분할해서 넣어야 한다.
  • 분할되어 저장된 파일은 App이 실행되었을 때 local 영역에다 하나의 파일로 합쳐서 복사해 두어야 할 필요가 있다. 이럴때는 다음 소스를 참고.
  • // Asset에 있는 번들 데이터(분할된 파일 형심)를 로컬 영역에 복사
    // 분할된 파일은 file.1, file.2, file.3, file.x ...
    // 위와 같은 형식으로 확장자 대신 번호가 붙어 있어야 한다.
    public static boolean copyToLocal(Context c, final String assetName, final String toPath) {
    int count = 1;
    InputStream in = null;

    FileOutputStream out = null;
    try {
    out = c.openFileOutput(toPath, Context.MODE_PRIVATE);
    }
    catch (FileNotFoundException e) {
    e.printStackTrace();
    }

    while (out != null) {
    // open src files
    try {
    in = c.getAssets().open(assetName + "." + String.valueOf(count));
    }
    catch (IOException e) {
    in = null;
    }

    if (in == null) break;

    // copy and merge
    byte[] buff = new byte[1024];
    int size = 0;

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

    in.close();
    }
    catch (IOException e) {
    e.printStackTrace();
    return false;
    }

    ++count;
    }


    try {
    out.close();
    }
    catch (IOException e) {
    e.printStackTrace();
    }

    return out != null;
    }

    반응형