簡體   English   中英

如何在android中訪問資產?

[英]how to access Assets in android?

編輯:修復

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

我從一個示例中獲得了一個解壓縮(decompress),其中將字符串作為壓縮文件的路徑。 但是由於我已經將文件保存在資產中,因此我需要從那里讀取文件...好了,到此為止。

不幸的是,它引發了我“ 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");

這將返回一個輸入流,您可以將其讀取到緩沖區。

// 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的實例傳遞給自定義構造函數,如果以后需要,可以將其分配給成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM