Android Write file permissions

Source: Internet
Author: User

First, add the user permission in the manifest.xml:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS" ></uses-permission>

Code,
String Path = "HTTP://192.168.1.22:8088/4";
URL url = new URL (path);
URLConnection cn = Url.openconnection ();
Cn.connect ();
InputStream stream = Cn.getinputstream ();
if (stream = = null)
throw new RuntimeException ("stream is null");
File temp = new file ("/data/data/com.dilifetech/mediaplayertmp");//com.dilifetech is the package name
Temp.createnewfile ();
String TempPath = Temp.getabsolutepath ();
FileOutputStream out = new FileOutputStream (temp);
byte buf[] = new byte[128];
do {
int numread = Stream.read (BUF);
if (numread <= 0)
Break
Out.write (buf, 0, Numread);
} while (true);
Mp.setdatasource (TempPath);
Stream.Close ();

file permissions in the Android system:
Android has its own security model, which can be found in the Android development documentation. When the application (. apk) is installed, a userid is assigned, and a userid match is required when the app is going to access other resources such as files. By default, any file created by the app, the database, sharedpreferences should be private (in/data/data/your_project/files/), and the rest of the programs cannot be accessed. Unless the mode_world_readable or mode_world_writeable are indicated at the time of creation, the rest of the programs will be properly accessed.

Because the way Android reads and writes files is secure, the process opens the file when Android requires the user ID of the process to be checked. So it can't be opened directly with the Java API, because the IO function of Java does not mention this mechanism. The program's private data cannot be opened directly with the Java API, the default path is/data/data/your_project/files/

    1. FileReader file = new FileReader ("Android.txt");

Special emphasis on private data here! The implication is that if a file or data is not private to the program, you can access it without having to go through Android's permission to check it, and then use the Java IO API for direct access. The so-called non-private data is only placed on the SDcard file or data, you can use the Java IO API to open the SDcard file directly.

    1. FileReader file = new FileReader ("/sdcard/android.txt");

If you want to open the program's own private files and data, you must use activity to provide openfileoutput and Openfileinput methods.

To create a file that is private to the program, you must use the Android read-write file method provided by activity due to permission requirements

    1. FileOutputStream OS = this.openfileoutput ("Android.txt", mode_private);
    2. OutputStreamWriter outwriter = new OutputStreamWriter (OS);

Read program-private files, due to permission requirements, must use the method provided by the activity

      1. FileInputStream os =this.openfileinput ("Android.txt");
      2. InputStreamReader Inreader = new InputStreamReader (OS);

http://wingjang.blog.163.com/blog/static/4791344220110142319369/

1     voidwritetoxml (String returnid) {2 3File =NewFile (Getactivity (). Getfilesdir (). GetPath () +File.separator4+FILENAME);5 6         if(File.exists ()) {--------------> is always False or true 7 8Documentbuilderfactory DBF =NULL;9Documentbuilder db =NULL;TenDocument doc =NULL; One             Try { ADBF =documentbuilderfactory.newinstance (); -                 //build Documentbuilder by Example -db =Dbf.newdocumentbuilder (); the                 //Create document to parse the given file -Doc =db.parse (file); -Element root =doc.getdocumentelement (); -  +Root.getelementsbytagname ("Invitations"). GetLength (); -Element Root2 = doc.createelement ("Invitation_" +Returnid); + Root.appendchild (ROOT2); A  atTransformerfactory TFS =transformerfactory.newinstance (); -Transformer tf =Tfs.newtransformer (); -Tf.transform (NewDomsource (DOC),NewStreamresult ( -                         Newfileoutputstream (file)); -}Catch(Exception e) { - e.printstacktrace (); in}finally { -Doc =NULL; todb =NULL; +DBF =NULL; -             } the}Else { *  $             Try {Panax NotoginsengXmlSerializer serializer =Xml.newserializer (); -StringWriter writer =NewStringWriter (); the Serializer.setoutput (writer); +Serializer.startdocument ("Utf-8",true); ASerializer.starttag ("", "invitations"); the  +Serializer.starttag ("", "Invitation_" +Returnid); -Serializer.text (""); $Serializer.endtag ("", "Invitation_" +Returnid); $  -Serializer.endtag ("", "invitations"); - serializer.enddocument (); theOutputStream out =getactivity (). Openfileoutput (FILENAME, - context.mode_private); ----------------->file.exists () is still true or false Wuyi getactivity (). Getfilestreampath (FILENAME). exists (); the  -OutputStreamWriter Outwriter =NewOutputStreamWriter (out); Wu Outwriter.write (writer.tostring ()); - writer.close (); About outwriter.close (); $ out.close (); -}Catch(FileNotFoundException e) { -  - e.printstacktrace (); A}Catch(IOException e) { +  the e.printstacktrace (); -             } $         } the}

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.