Get asset text content and read pictures

Source: Internet
Author: User

1  ImportAndroid.content.Context;2 ImportAndroid.content.res.AssetManager;3 ImportAndroid.graphics.Bitmap;4 Importandroid.graphics.BitmapFactory;5 6 ImportJava.io.BufferedReader;7 ImportJava.io.ByteArrayOutputStream;8 Importjava.io.IOException;9 ImportJava.io.InputStream;Ten ImportJava.io.InputStreamReader; One  A  -  Public classLocalfileutils { -  the     /** - * Get Asset text content -      * @paramContext -      * @paramStr +      * @return -      */ +      Public Final Staticstring Getstringformasset (context context, String str) { ABufferedReader in =NULL; at         Try { -in =NewBufferedReader (NewInputStreamReader (Context.getassets (). Open (str) )); - String Line; -StringBuilder buffer =NewStringBuilder (); -              while(line = In.readline ())! =NULL) { -Buffer.append (line) append (' \ n ')); in             } -             returnbuffer.tostring (); to}Catch(IOException e) { + e.printstacktrace (); -             return""; the}finally { *             if(In! =NULL) { $                 Try {Panax Notoginseng in.close (); -in =NULL; the}Catch(IOException e) { + e.printstacktrace (); A                 } the             } +         } -     } $  $     /** - * @description reading pictures from assets -      * the      * @paramContext -      * @paramFileNameWuyi      * @returnImage the * @date 2015-6-11 15:00:55 -      */ Wu      Public StaticBitmap Getimagefromassetsfile (context context, String FileName) { -Bitmap image =NULL; AboutAssetmanager am =context.getassets (); $InputStream is =NULL; -         Try { -is =Am.open (fileName); -Image =Bitmapfactory.decodestream (IS); A             returnimage; +}Catch(IOException e) { the e.printstacktrace (); -             returnimage; $}finally { the             if(Is! =NULL) { the                 Try { the is.close (); theis =NULL; -}Catch(IOException e) { in e.printstacktrace (); the                 } the             } About         } the     } the  the     /** + * Get raw text content -      * @paramContext the      * @paramRawidBayi      * @return the      */ the      Public Final StaticString Getstringformraw (Context context,intRawid) { -Bytearrayoutputstream BAOs =NULL; -InputStream in =context.getresources (). Openrawresource (Rawid); the         Try { theBAOs =NewBytearrayoutputstream (); the             byte[] buffer =New byte[1024]; the             intLen = 0; -              while(len = in.read (buffer))! =-1) { theBaos.write (buffer, 0, Len); the             } the baos.close ();94             returnbaos.tostring (); the}Catch(Exception e) { the e.printstacktrace (); the             return"";98}finally { About             if(BAOs! =NULL) { -                 Try {101 baos.close ();102BAOs =NULL;103}Catch(IOException e) {104 e.printstacktrace (); the                 }106             }107         }108     }109  the}

1 LongStartTime4 =System.currenttimemillis ();2String My_json = Localfileutils.getstringformasset ( This, "Testbean1.json");3  for(intn = 0; n < 100000; n++) {4    //encapsulating a JSON string into an entity class using the JSON manipulation tool5TestBean1 Tobean = My_gson.fromjson (My_json, My_type);//Jsontool.tobean (JSON, testbean1.class);6 System.out.println (Tobean);7 }8 LongEndTime4 = System.currenttimemillis ()-StartTime4;9LOG.I (TAG, "Gson ..." + endTime4);

Get asset text content and read pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.