Write data to phone memory

Source: Internet
Author: User

Of course it's written to the phone in the form of a file, but it's slow to run on the emulator.

Layout file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.file.MainActivity" > <textview android:id= "@+id/textview1" Android:layout_wid Th= "Fill_parent" android:layout_height= "wrap_content" android:text= "file name"/> <edittext Andro Id:id= "@+id/filename" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android : layout_below= "@id/textview1"/> <textview android:id= "@+id/textview2" android:layout_width= "fill_p Arent "android:layout_height=" Wrap_content "Androidoid:layout_below= "@id/filename" android:text= "text content"/> <!--input Yu display at least 3 lines--<edittext Andro Id:id= "@+id/content" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android: layout_below= "@id/textview2" android:minlines= "3"/> <button android:id= "@+id/save" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/content" a        ndroid:text= "Save"/> <textview android:id= "@+id/filecontent" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_below= "@id/save" android:text= "What you entered:"/></rela Tivelayout>
Business logic for saving data

Package Com.example.service;import Java.io.fileoutputstream;import Android.content.context;public class FileService {    private context context;    Public Fileservice (Context context) {    This.context = context;    }    public void Save (String filename,string content) {    try {    /*openfileoutput     * First parameter: Input file name, no receive path     * Second parameter: Permission to read the file     * The app will be saved by default in the Project Files folder, where the Files folder will be automatically generated after saving the data     */fileoutputstream OutStream = Context.openfileoutput (filename, context.mode_private);//data is stored in bytecode mode Outstream.write (Content.getbytes ()); O Utstream.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}      
Package Com.example.file;import Com.example.service.fileservice;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class MainActivity Extends Activity {private EditText filename;private EditText content;private Button save;private TextView filecontent;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); fileName = (EditText) Findviewbyid (r.id.filename); content = (EditText) Findviewbyid ( r.id.content); save = (Button) Findviewbyid (r.id.save); filecontent = (TextView) Findviewbyid (r.id.filecontent); Save.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated method    Stubfileservice service = new Fileservice (mainactivity.this); try {service.save (Filename.gettext (). toStRing (), Content.gettext (). toString ()); Toast.maketext (Getapplicationcontext (), "saved successfully", Toast.length_short). Show ();    catch (Exception e) {toast.maketext (Getapplicationcontext (), "Save Failed", Toast.length_short). Show ();    String str = "What you entered:" + content.gettext (). toString (); Filecontent.settext (str);}});}}

Click Save to find your project in the Ddms view of Date/data. Under the project will generate a Files folder, the saved data file is changed folder, you can click the icon in the upper right corner to export view


Write data to phone memory

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.