2011-09-04 50 views
2

編輯:用如何訪問Android中的資產?

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

固定它我從一個例子,在那裏它以一個字符串作爲到壓縮文件的路徑的unzipper(解壓縮)。但是由於我在我的資產中有這個文件,我不知何故需要從那裏讀取它......好吧,有這麼多。

不幸的是它引發了我「錯誤/解壓縮(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(); 

} 

} 

謝謝!

回答

4

使用您的上下文:

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

這將返回你可以閱讀到一個緩衝的輸入流。

// 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()因爲Activity延伸Context。如果您不在活動內部工作並在稍後需要時將其分配給成員,則還可以將Context的實例傳遞給自定義構造函數。

+0

謝謝!還有一件事,如果我不是在一個活動中,而是在一個班級中,我如何獲得上下文。見上面編輯。 – Roger

+0

如果您在「Activity」中實例化,請爲您的類創建一個自定義構造函數,該函數將一個「Context」作爲參數,您可以在其中將「this」從您的活動中傳遞。您可以將其設置爲「Context」類型的成員。如果你還可以沿着一系列構造函數或設置器傳遞上下文,如果需要的話。如果你真的沒有從一個活動中創建,或者你沒有'Context'來處理,你可能會做一些壞事,但你可以嘗試調用'getApplicationContext()';該文檔是在這裏 - http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext() – SK9

+0

我更簡單一些,通過將解壓縮到它自己的活動,從服務調用。見上面的編輯。現在唯一的問題是它拋出「java.lang.ClassCastException:android.content.res.AssetManager $ AssetInputStream」。 – Roger

相關問題