登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 906|回复: 0

【Android教程】把assets文件复制到本地

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-6-20 20:26:40 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. 把assets文件复制手机储存中
    ) Z+ J, V& b" t8 W  }7 @/ F
  2. ' @0 F& j- ]5 B  X
  3.       try
    # S  r) Z6 [. S# r+ z
  4.                         {
    " v5 [7 I9 c- Q7 O5 O- q% j& k: {
  5.                                 unzip(getAssets().open("d.zip"), "sd路径");
    3 N$ f8 p3 z# K7 [8 k' U
  6.                         }
    0 T9 f. X! C5 ?7 @2 g3 R
  7.                         catch (IOException e); w; G5 k4 j, e$ y$ _4 q
  8.                   {}. K4 U  x3 b  u3 A& e

  9. / G% w. D2 T/ L% ~( h4 o' e6 A5 F
  10. //$ p3 g, K4 z( i5 }. U/ }% e' l
  11. public static void unzip (InputStream is, String dir) throws IOException  : b& R/ g3 l3 Q. `; G' n" Z/ s
  12.     {  ' x8 {. }0 T4 ?  q3 I
  13.         File dest = new File(dir);  4 |" {6 L  Z7 n" g
  14.         if ( !dest.isDirectory())   
    0 C+ \( f2 {) Z
  15.             throw new IOException("Invalid Unzip destination " + dest);  - ~" ?$ i( o' @
  16. - n% P3 l, s6 d, Y$ ~
  17.         if(null == is){  % }/ n  c' o" Y1 w0 D! S
  18.             throw new IOException("InputStream is null");  6 s2 ?* H3 N- [, s
  19.         }  * k5 @( X& X( g. _  \' F) Q
  20. 5 r' O& q4 t; g5 d
  21.         ZipInputStream zip = new ZipInputStream(is);  ) ?  x' a. X4 r' y; @

  22. + C- \' ]. A  h$ D" {
  23.         ZipEntry ze;  : {" k' _3 R$ n& F
  24. 0 k2 h; W5 w3 G7 {, c7 n+ S
  25.         while ( (ze = zip.getNextEntry()) != null ) {  . w, H* `& V$ t8 \4 {9 H
  26.             final String path = dest.getAbsolutePath()   ! J, _  A/ @* s, @4 v* M7 W  U
  27.                 + File.separator + ze.getName();  
    " t3 a8 \. E% x9 ?: q3 t- L: y$ K* K  j
  28. ' g  J% g' T1 z' f# _3 G# o0 L
  29.             // Create any entry folders  4 Y7 y* a; T( U+ N! h; c
  30.             String zeName = ze.getName();  
    3 F- i1 |+ W% w$ z! O- L' U% _
  31.             char cTail = zeName.charAt(zeName.length() - 1);  
    7 d1 O( u$ Y/ A6 V' l
  32.             if ( cTail == File.separatorChar)   
    5 [% Q4 A: i+ O9 ]! m3 [8 n
  33.             {     3 d$ K' K  }* j- p) l1 K! `
  34.                 File file = new File(path);  ) K- K( X( `& E% a& f
  35.                 if(!file.exists()){  % }0 v1 {/ \6 s
  36.                     if ( !file.mkdirs() ){  1 z$ o) u( L) {4 F  l
  37.                         throw new IOException("Unable to create folder " + file);     
    % O% x6 c8 t. G( G9 y
  38.                     }  
    1 ~( ~; g; }) a8 J7 n) G7 ]
  39.                 }  
    1 {3 C6 Y9 t$ ]6 ?* [" A9 z' Z2 B
  40.                 continue;  
    " X* W, P* J4 ^( Y+ N1 h) Y! D
  41.             }  $ \% p. \% r7 U2 f9 C

  42. 3 L& q0 R5 i. u3 m# j% ~1 M, a2 R
  43.             FileOutputStream fout = new FileOutputStream(path);  3 M( ?1 B7 s, \2 r0 D- L
  44.             byte[] bytes = new byte[1024];  5 @$ p4 d- i/ Y8 z, A: N. I! m- B
  45.             int c;  
    ( R! i. ~4 e! `+ d( a9 j
  46.             while((c = zip.read(bytes)) != -1){  
    ) j. |( B9 l. d9 v% N$ S, }! g
  47.                 fout.write(bytes,0, c);    3 }# k( i( o$ {+ K, `
  48.             }  0 v. a( O" x9 n; i) Y

  49. ; P+ ?  H' G. `
  50.             zip.closeEntry();  " K  C& x- p( ]; h
  51.             fout.close();              
    / K1 T8 G6 P# [
  52.         }  * {7 k( Q" [$ V
  53. 7 ?* y0 j  J& Q; V3 d+ X* P
  54.     }3 i- ~6 X% c/ u% X% u) |: S: Q
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-5-6 07:56 , Processed in 0.060609 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表