2011-09-04 2 views
2

편집 : 나는 그것이 압축 된 파일의 경로와 같은 문자열을 사용합니다 예를 들어,에서 unzipper (압축 해제)를 가지고android에서 애셋에 액세스하는 방법은 무엇입니까?

ZipInputStream zin = new ZipInputStream(getAssets().open("totalkeys.zip")); 

로 고정. 하지만 제가 자산에 파일을 가지고 있기 때문에 어떻게 든 그것을 읽을 필요가 있습니다 ... 글쎄, 지금까지 이걸 가지고있어.

불행히도 "ERROR/Decompress (24122) : java.lang.ClassCastException : android.content.res.AssetManager $ AssetInputStream"어떤 생각을 고칠 수 있습니까?)

public class dec extends Activity { 
/** Called when the activity is first created. */ 
@Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     Toast.makeText(this, "hello, starting to unZipp!", 15500).show(); 

     String location = Environment.getExternalStorageDirectory() + "/unzipped/"; 
     ///////////////////////////////////////////////////////////////////// 

     try { 

      AssetManager mgr = getBaseContext().getAssets(); 

      FileInputStream fin = (FileInputStream)mgr.open("totalkeys.zip"); 
      // throws ERROR/Decompress(24122): java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream 

      //FileInputStream fin = new FileInputStream(_zipFile); /// old one, that wanted a String. 
      ZipInputStream zin = new ZipInputStream(fin); 
      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
       Log.v("Decompress", "Unzipping " + ze.getName()); 

       if(ze.isDirectory()) { 
       dirChecker(ze.getName()); 
       } else { 
       FileOutputStream fout = new FileOutputStream(location + ze.getName()); 
       for (int c = zin.read(); c != -1; c = zin.read()) { 
        fout.write(c); 
       } 

       zin.closeEntry(); 
       fout.close(); 
       } 

      } 
      zin.close(); 
      } catch(Exception e) { 
      Log.e("Decompress", "unzip", e); 
      } 

     } 

     private void dirChecker(String dir) { 

      String location = Environment.getExternalStorageDirectory() + "/unzipped/"; 
      File f = new File(location + dir); 

      if(!f.isDirectory()) { 
      f.mkdirs(); 
      } 

     //////////////////////////////////////////////////// 

     finish(); 

} 

} 

고마워요!

InputStream is = myContext.getAssets().open("totalkeys.zip"); 

이 당신이 버퍼에 읽을 수있는 입력 스트림을 반환

답변

4

은 컨텍스트를 사용합니다. 당신이 활동에서 작업하는 경우 ActivityContext를 확장하기 때문에

// Open the input stream 
InputStream is = mContext.getAssets().open(FILE_NAME); 

byte[] buffer = new byte[1024]; 
int length; 
while ((length = is.read(buffer))>0){ 
      // write buffer some where, e.g. to an output stream 
      // as 'myOutputStream.write(buffer, 0, length);' 
} 
// Close the stream 
try{ 
    is.close(); 
} catch(IOException e){ 
    Log.e(this.getLocalClassName().toString(), e.getMessage()); 
    //this.getLocalClassName().toString() could be replaced with any (string) tag 
} 

, 당신은 this.getAssets()를 사용할 수 있습니다. 또한 액티비티 내부에서 작업하지 않는 경우 Context의 인스턴스를 사용자 정의 생성자에 전달하고 나중에 필요할 경우 멤버에게 할당 할 수 있습니다.

+0

감사합니다. 한 가지만 더, 내가 활동에 있지 않다면 컨텍스트를 얻으려면 어떻게해야합니까? 위에서 편집 참조. – Roger

+0

'Activity' 내부에서 인스턴스를 생성하는 경우 클래스에 대한 사용자 정의 생성자를 생성합니다.이 생성자는 액티비티에서'this'를 전달할 수있는'Context'를 인수로 취합니다. 이것을 'Context' 타입의 멤버로 설정할 수 있습니다. 필요한 경우 생성자 체인 또는 설정자 체인을 따라 컨텍스트를 전달할 수도 있습니다. 액티비티로 정말로 생성하지 않거나'Context'가 없다면 아마 나쁜 일을하고있을 것이지만'getApplicationContext()'를 호출 해 볼 수 있습니다. doc은 여기에 있습니다 - http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext() – SK9

+0

unzipper를 자신의 활동에 넣으면서 간단하게 만들었습니다.이 활동은 서비스에서 호출됩니다. 위의 편집을 참조하십시오. 지금 당장 유일한 문제는 "java.lang.ClassCastException : android.content.res.AssetManager $ AssetInputStream"을 던졌습니다. – Roger

관련 문제