Android access to files under assets

Source: Internet
Author: User

Assets often can put some relatively large resources, for these resources how we access.

Steps

1. Get Assetmanager.
Assetmanager am = getresources (). Getassets ();
2. Open the corresponding input stream with the Assetmanager's open (String FilePath) method.
InputStream is = Am.open (assetsfilename);

Read picture file save to SD card sample code
 PublicBooleanSavetosdcard(String Localfilepath, String fileName, Bitmap Bitmap) {String Extrapath = Environment.getexternalstoragedirectory (). toString ();//Use absolute pathExtrapath = Extrapath +"/"+"PaperCut"+"/"+localfilepath;//String path = null;File File =NewFile (Extrapath);if(!file.exists ()) file.mkdirs ();Try{FilePath = File.getpath () +"/"+ FileName +". png"; File NewFile =NewFile (FilePath);if(Newfile.exists ()) {Toast.maketext (Getapplicationcontext (), R.string. FINISHTIPS1,//have saved a proToast.length_short). Show ();return true; }Else{FileOutputStream FileOutputStream =NewFileOutputStream (FilePath); Bitmap.compress (Bitmap.CompressFormat.PNG, -, FileOutputStream);//end of another format                //Bitmap.compress (Compressformat.png, fileoutputstream);Fileoutputstream.flush ();            Fileoutputstream.close (); }        }Catch(Exception e) {E.printstacktrace ();//System.out.println (e.tostring ());            return false; }return true; }PrivateBitmapGetimagefromassetsfile(intPosition) {//bitmap image = null;Bitmap image =NULL; String Assetsfilename =NULL; Assetsfilename ="album/"+ is. Imagemychoosename.Get(position) +"/1.png";//assetmanager getting resources from the assets folderAssetmanager am = getresources (). Getassets ();Try{InputStream is= Am.open (Assetsfilename);//Generate image from InputStream decodingImage = Bitmapfactory.decodestream ( is); is. Close (); }Catch(IOException e)            {E.printstacktrace (); }returnImage }//CallSavetosdcard ("album","FilePath"), Getimagefromassetsfile (position));

Android access to files under assets

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.