Android file storage and Reading

Source: Internet
Author: User

I. Permissions ISSUES

The storage space in the phone is divided into ROM and sdcard,rom with the operating system and the apps we install, and the data generated by our app is generally placed in sdcard. Of course, Android also creates a data storage space for each app in the ROM, with the specific directory under/data/data. In real-Time debugging, this directory does not have any permissions on other users, so we cannot open the directory in DDMS.

The solution is that we log on to our phone via adb, and then switch directly to the root user, and then the phone may ask for authorization, we choose to allow. This will make us the root user. Then we are changing the permissions on the/data directory so that we can see the contents of/data on the DDMS. The specific operation is as follows:

Second, layout settings
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://"Xmlns:tools= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"android:orientation= "vertical"Tools:context= "">    <EditTextAndroid:id= "@+id/username"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter user name"/>    <EditTextAndroid:id= "@+id/password"Android:inputtype= "Textpassword"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter password"/>    <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal">        <CheckBoxAndroid:id= "@+id/remember"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Remember user name and password"android:layout_centervertical= "true"/>        <ButtonAndroid:id= "@+id/login"Android:layout_alignparentright= "true"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Login"/>    </Relativelayout></LinearLayout>
Third, Java code;;ImportAndroid.os.Bundle;ImportAndroid.telephony.SmsManager;ImportAndroid.util.Log;ImportAndroid.util.PrintWriterPrinter;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.CheckBox;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;;;;;;;Importjava.util.List; Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button BT=(Button) Findviewbyid (; Bt.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {EditText etusername=(EditText) Findviewbyid (; String username=Etusername.gettext (). toString (); EditText Etpassword=(EditText) Findviewbyid (; String Password=Etpassword.gettext (). toString (); LOG.E ("Login", "Logon succeeded"); CheckBox CB=(CheckBox) Findviewbyid (; if(cb.ischecked ()) {String path= "/data/data/"; File File=NewFile (path); PrintWriter PW=NULL; Try{PW=NewPrintWriter (NewBufferedWriter (NewOutputStreamWriter (Newfileoutputstream (file))); } Catch(FileNotFoundException e) {LOG.E ("Mainactivity", "File not Found");                    } pw.println (username);                    PW.PRINTLN (password);                Pw.close ();    }            }        }); }}

Android creates a folder under/data/data for each app, and the name of the folder is the app's package name. Our app is, so in order to save the user data, I create a infro.txt folder in/data/data/ then save the user name and password package to that folder. The user name and password are saved using the normal Java IO stream.

Android file storage and Reading

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: 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.