How to access an Android database file to a memory card _android

Source: Internet
Author: User
Tags file permissions stringbuffer

Nonsense not much said, directly to everyone to paste the code, the specific code as follows

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical "
> <button android:id= "@+id/save" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Save data (File)"/> <button android:id= "@+id/read" android:layout_width= "Wrap_content" Android:
layout_height= "Wrap_content" android:text= "read data (File)"/> </LinearLayout> package Com.example.yanlei.wifi;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.PrintStream;
Import Java.util.Scanner; public class MainactiviTy extends appcompatactivity {private button btnsave=null; private button btnread=null; private File file=null; private s
Tatic final String filename= "Data.txt"; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Btnsave= (Button) Super.findviewbyid (R.id.save);

Btnread= (Button) Super.findviewbyid (R.id.read);

Btnsave.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {PrintStream ps=null; Determine if the external storage card exists if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext Getapplicationcontext ( ), "Read failed, SD memory card does not exist!"
", Toast.length_long). Show ();
Return ///Initialize File String path=environment.getexternalstoragedirectory (). toString () +file.separator + "Genwoxue" +
File.separator +filename;

File=new File (path);
If the parent folder for the current file does not exist, create the Genwoxue folder if (!file.getparentfile (). exists ()) File.getparentfile (). Mkdirs (); Write file try {PS = new PrintStream (new FileOutputStream (file)); Ps.println ("Follow me to learn Web site: www.genwoxue.com");
Ps.println ("");
Ps.println ("e-mail: hello@genwoxue.com"); catch (FileNotFoundException e) {e.printstacktrace ();}
finally{ps.close ();} Toast.maketext (Getapplicationcontext (), "Save success!"
", Toast.length_long). Show ();});

Btnread.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {stringbuffer info=new stringbuffer (); Determine if the external storage card exists if (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {Toast.maketext Getapplicationcontext ( ), "Read failed, SD memory card does not exist!"
", Toast.length_long). Show ();
Return ///Initialize File String path=environment.getexternalstoragedirectory (). toString () +file.separator + "Genwoxue" +
File.separator +filename;

File=new File (path); if (!file.exists ()) {Toast.maketext (Getapplicationcontext ()), "file does not exist!"
", Toast.length_long). Show ();
Return
//Read the contents of the file Scanner scan=null;
try {scan=new Scanner (new FileInputStream (file)) while (Scan.hasnext ()) {Info.append (Scan.next ()). Append ("☆☆☆\n"); Toast.maketext (Getapplicationcontext(), info.tostring (), Toast.length_long). Show (); catch (FileNotFoundException e) {e.printstacktrace ();}
finally{scan.close ();}
}); }
}

Permissions

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.example.yanlei.wifi > <!--Create and delete file permissions in SDcard--> <uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/> <!--Write data permissions to SDcard--> <uses-permission android:name=" Android.permission.READ_EXTERNAL_STORAGE "/> <uses-permission android:name=" android.permission.WRITE_ External_storage "/> <application android:allowbackup=" true "android:icon=" @mipmap/ic_launcher "Android:label = "@string/app_name" android:theme= @style/apptheme "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </ intent-filter> </activity> </application> </manifest> <uses-permission android:name= " Android.permission.READ_EXTERNAL_STORAGE "/> <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> 

The above is a small part of the introduction of the Android database file access to the storage card method, I hope to help you, this article is not good to write please heroes forgive me!

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.