Phone internal file Storage--assets Directory

Source: Internet
Author: User

One. Create

1./src/main/assets

2. Switch to Project view mode and create a new folder under Main assets

Two. Features

1. Same level as Res

2. Mainly to store large files in the project

3. File is not managed by R class

Three. API

1.AssetManager Asset Manager

1-getassets () Get asset manager

2-open (file name) returns the InputStream of the file

2.imageview:setimagebitmap (Bitmap instance) to set a bitmap for a picture view

3.Bitmap bitmap: bitmapfactory.decodefile (picture file path) Use factory method to get Bitmap of picture file.

The image internal storage code shows:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp3.MainActivity" One android:orientation= "vertical"> A  -  -  the     <Button -         Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "Save asset file to internal storage" + Android:onclick= "Bt4_onclick"/> -  +     <ImageView A         Android:layout_width= "Wrap_content" at Android:layout_height= "30DP" - Android:id= "@+id/iv_1" - android:src= "@drawable/f9"/> -  -     <Button -         Android:layout_width= "Match_parent" in Android:layout_height= "Wrap_content" - Android:text= "Set picture pointing to internal storage" to Android:onclick= "Bt5_onclick"/> +  -  the  *  $ Panax Notoginseng </LinearLayout>
picture Phone internal storage
1  Packagecom.hanqi.testapp3;2 3 Importandroid.content.SharedPreferences;4 ImportAndroid.content.res.AssetManager;5 ImportAndroid.graphics.Bitmap;6 Importandroid.graphics.BitmapFactory;7 Importandroid.os.Environment;8 Importandroid.support.v7.app.AppCompatActivity;9 ImportAndroid.os.Bundle;Ten ImportAndroid.text.BidiFormatter; One ImportAndroid.view.View; A ImportAndroid.widget.EditText; - ImportAndroid.widget.ImageView; - ImportAndroid.widget.TextView; the ImportAndroid.widget.Toast; -  - ImportJava.io.File; - ImportJava.io.FileInputStream; + ImportJava.io.FileOutputStream; - ImportJava.io.InputStream; + ImportJava.io.PrintStream; A  at  -  Public classMainactivityextendsappcompatactivity { -  -    - ImageView iv_1; -  in @Override -     protected voidonCreate (Bundle savedinstancestate) { to         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -  the  *iv_1=(ImageView) Findviewbyid (r.id.iv_1); $ Panax Notoginseng     } -  the  +  A     //Saving asset files to internal storage the      Public  voidBt4_onclick (View v) +     { -         Try { $  $             //manipulating assets Directory files -  -             //1. Get Assetsmanager the  -Assetmanager am=getassets ();Wuyi  the             //2. Manipulate the asset catalog, read-write side-by- side -             //1) Read file to memory InputStream WuInputStream Is=am.open ("Car.png"); -  About  $             //2) Write file to directory OutputStream -FileOutputStream fos=openfileoutput ("Test.png", mode_private); -  -             //Read and write first A             byte[] b=New byte[1024]; +             intI=0; the  -              while((I=is.read (b)) >0) $             { theFos.write (b,0, i); the             } the  the fos.close (); - is.close (); in  theToast.maketext (mainactivity. This, "Save file succeeded", Toast.length_short). Show (); the  About  the  the         } the         Catch(Exception e) +         { -Toast.maketext (mainactivity. This, "Error saving file", Toast.length_short). Show (); the         }Bayi     } the  the     //set a picture to point to internal storage -      Public voidBt5_onclick (View v) -     { the  the         //1. Get the file path theString Path=getfilesdir (). GetAbsolutePath () + "/test.png"; the  -Toast.maketext (mainactivity. This, "path=" +path, Toast.length_long). Show (); the  the         //2. Get Bitmap,bitmapfactory.decodefile ("File path") from the image stored internally; theBitmap bm=bitmapfactory.decodefile (path);94  the         //3. Set picture source for picture View the Iv_1.setimagebitmap (BM); the     }98  About    -}
picture Phone internal storage. Java

Phone internal file Storage--assets Directory

Related Article

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.