android_10_文件访问权限

效果:

 

android_10_文件访问权限_第1张图片

 

代码:

 

 

 

    public void privateFile(){
        //路径已经默认为data/data/包名/files
        try {
            FileOutputStream fos = openFileOutput("privateFile.txt", MODE_PRIVATE);
            fos.write("beyond".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void readFile(){
        //路径已经默认为data/data/包名/files
        try {
            @SuppressWarnings("deprecation")
            FileOutputStream fos = openFileOutput("readFile.txt",  MODE_WORLD_READABLE);
            fos.write("beyond".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void writeFile(){
        //路径已经默认为data/data/包名/files
        try {
            @SuppressWarnings("deprecation")
            FileOutputStream fos = openFileOutput("writeFile.txt",  MODE_WORLD_WRITEABLE);
            fos.write("beyond".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void readWriteFile(){
        //路径已经默认为data/data/包名/files
        try {
            @SuppressWarnings("deprecation")
            FileOutputStream fos = openFileOutput("readWriteFile.txt", MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
            fos.write("beyond".getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

 

 

 

 

 

 

 

 

另一个应用(去读取rw文件)

 

public void accessToReadWriteFile(View v){
        File file = new File("data/data/com.sg31.globalFilePermission/files/readWriteFile.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            Toast.makeText(this, br.readLine(), 0).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android_数据存储)