Android file access rights mode configuration

Source: Internet
Author: User

Three kinds are used:

Mode_private/mode_append: The two are each start to re-write, one is the append mode. But the access rights are the same for both.

Mode_world_readable: Global readable mode, can be read by other applications, not recommended

Mode_world_writeable: Global writable mode, can be read by other applications, not recommended

The code tests are as follows:

 Packagecom.swust.access;ImportJava.io.FileOutputStream;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }    //button One Click event     Public voidonclick (View v) {//path already defaults to data/data/package name/files//private and Append permissions are the same        Try{FileOutputStream fos= Openfileoutput ("Info.txt", mode_private); Fos.write ("I'm the King of Bubbles.". GetBytes ());                    Fos.close (); }Catch(Exception e) {e.printstacktrace (); }    }        //button 2 Click event         Public voidOnclick1 (View v) {//Global readable mode, not recommended            Try{FileOutputStream fos= Openfileoutput ("Info1.txt", mode_world_readable); Fos.write ("Global readable mode, not recommended". GetBytes ());                            Fos.close (); }Catch(Exception e) {e.printstacktrace (); }        }        //Button 3 Click event                 Public voidOnclick2 (View v) {//Global writable mode, not recommended                    Try{FileOutputStream fos= Openfileoutput ("Info2.txt", mode_world_writeable); Fos.write ("Global writable mode, not recommended". GetBytes ());                                            Fos.close (); }Catch(Exception e) {e.printstacktrace (); }                }}

Effect:

When the third button of the app is written to read-write (warning: Not used, here for testing)

The code is as follows, using the OR statement connection:

  button 3 Click event  public  void   Onclick2 (View v) {
    //  global writable readable mode, deprecated  try  {fileoutputstream fos  = openfileoutput ("Info2.txt" , mode_world_writeable |                        mode_world_readable); Fos.write ( Global writable and readable, where the next app is tested to read the files of the Access app.                        The use of ".getbytes ()) is not recommended;                                            Fos.close ();  catch   (Exception e) {E.PR                    Intstacktrace (); }                }

At this time

Build another Android app to test:

Code, by the way, by using the file method to read files, used by the project:

//Hazard testing: To read readable and writable files from other applications//By the way, read by using file files (for projects)     Public voidonclick (View v) {//access to other application file paths must be handwritten, because the API can only access the application'sFile File =NewFile ("Data/data/com.swust.access/files/info2.txt"); Try{FileInputStream fis=Newfileinputstream (file); BufferedReader BR=NewBufferedReader (NewInputStreamReader (FIS)); Toast.maketext ( This, Br.readline (), 1). Show (); }Catch(Exception e) {e.printstacktrace (); }    }

Effect:

As you can see, files that can be accessed to other apps are generally not recommended!

Android file access rights mode configuration

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.