饒榮慶 -- 您今天UCWEB了嗎?--http://www.ucweb.com

          3G 手機(jī)開發(fā)網(wǎng)

             :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            99 Posts :: 1 Stories :: 219 Comments :: 0 Trackbacks

          原文 http://www.3geye.net/?3/viewspace-3021

           

          你想像j2me那樣直接把所有的資源文件放在src目錄下面然后通過getClass().getResourceAsStream(name)這么簡單就可以獲取資源文件嗎。3GEYE h$j nN4]4f2D8HK
          答案是很肯定的。下面看看代碼吧。3GEYE^ n!}_ V8b

          importcom.google.android.samples.R;3GEYE5mDZ
          qsK
          Teu~
          3GEYE-h4QC*} {k
          importandroid.app.Activity;3GEYEZ,x |em-N(h%r*r:m
          importandroid.os.Bundle;
          &xM&C1_1\"a3
          importandroid.widget.TextView;3GEYEP|e5H2qb
          3GEYE7v6s ox/yH
          importjava.io.IOException;3GEYE?T~Yk`D n YEu
          importjava.io.InputStream;
          (AK.nmQ2j%Hi33GEYEu@;K#[V'G Nx

          5G;B5Px p-J3
          /**3GEYETL/Zs-X7r\%d&V Vh
           * Demonstration of styled text resources.
          8z1pj ifgz:g+t8g Q#Fx3 */

          c9B,D.{S PAq.p3
          publicclassReadAssetextendsActivity
          iO+j,TQ J x&V3
          {
          Z9H+j~"E'vdJ|3   
          @Override
          &I]@{*fl0QF/\3       
          protectedvoidonCreate(Bundleicicle)3GEYEw&C3Z%@Wg%x1Kk
             
          {
          9|Mx Lw%y Y,d3       
          super.onCreate(icicle);
          2_3Qi;Mf SK n]2J3
          3ua:d;N PV3       
          // See assets/res/any/layout/styled_text.xml for this
          1_a I X"^ k&pT3       
          // view layout definition.3GEYEX}$Vf8y9E$G X
                  setContentView
          (R.layout.read_asset);
          )x e ~(V~4t mE3
          \Y9C,m-cI|J,F3       
          // Programmatically load text from an asset and place it into the3GEYEIa$hWB0H3GW5s
                 
          // text view.  Note that the text we are loading is ASCII, so we
          {:E~Z?H2T3       
          // need to convert it to UTF-16.
          y\1~R9^ Yx3       
          try{
          fi*l,Lp#T3           
          InputStreamis=getAssets().open("read_asset.txt");
          .B `o?q+o t}3
          Um4MG nt8p4k3           
          // We guarantee that the available method returns the total
          aM7G;`U2Dt.h%px5|3           
          // size of the asset...  of course, this does mean that a single
          }d?F~$v __'t8}3           
          // asset can't be more than 2 gigs.3GEYE,O?@'PG)XO
                     
          intsize=is.available();
          }$~4g Vf V3
          H/Re%P6EX3           
          // Read the entire asset into a local byte buffer.3GEYE\-y#M.hh;~ ny{ DG
                     
          byte[]buffer=newbyte[size];
          Z#x/U"G"kH3           
          is.read(buffer);
          hfc6AwFxbg+]D3           
          is.close();3GEYE??ht,b&u5x
          3GEYE_lx [D9L q|7rs
                     
          // Convert the buffer into a Java string.
          )k-_6d-I~sVR3           
          Stringtext=newString(buffer);
          y7|4N-Q*a:c33GEYE(E*YSD:am wW$x5z
                     
          // Finally stick the string into the text view.
          .}qktl|&V X\3           
          TextViewtv=(TextView)findViewById(R.id.text);3GEYE;V d MJbL X~(hT
                      tv
          .setText(text);3GEYEx"jE"k q!V2UV,y?tlV
                 
          }catch(IOExceptione){3GEYE:{om A;o7s.pZ1@$IW
                     
          // Should never happen!
          In2I ?[3           
          thrownewRuntimeException(e);
          X4x%vo$h?W:r3       
          }
          $A*pS8qR;}3   
          }3GEYEgA_s A |V
          }3GEYE%aqVh.\T c

          F?K@Fzn/s3_%zw3你只需要把你的資源文件放到assets目錄下面。一切就是那么的簡單,容易。我開始有點(diǎn)喜歡Android。3GEYE._g&A0bp\

          o9k o+x-{x/g-jD3好了,我的J2ME的程序也逐步移植完成了。

           



          爬蟲工作室 -- 專業(yè)的手機(jī)軟件開發(fā)工作室
          3G視線 -- 專注手機(jī)軟件開發(fā)
          posted on 2008-03-08 21:55 3G工作室 閱讀(1261) 評論(2)  編輯  收藏

          Feedback

          # re: Android處理非res資源的例子 2008-03-11 22:48 千里冰封
          還是用標(biāo)準(zhǔn)的J2ME好  回復(fù)  更多評論
            

          # re: Android處理非res資源的例子 2009-12-18 17:00 GOOD
          好用.謝謝  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 碌曲县| 贵阳市| 屏东市| 环江| 南安市| 斗六市| 五峰| 双鸭山市| 黄浦区| 沅江市| 大港区| 东辽县| 拜泉县| 河西区| 孟村| 瑞丽市| 沂源县| 海城市| 彭阳县| 陇川县| 大安市| 五华县| 介休市| 德昌县| 出国| 双桥区| 尼木县| 鄯善县| 九江县| 晴隆县| 惠水县| 隆德县| 大宁县| 长葛市| 江达县| 阳新县| 濮阳县| 平度市| 永修县| 新巴尔虎左旗| 尚义县|