简体   繁体   中英

Create File into system android 4.4.2

I don't know why since I switched to kitkat my app cannot create any file in system (This method was working on android 4.1.2). My app has su permissions, mount system as RW and READ and WRITE_EXTERNAL_STORAGE permissions declared in manifest. Logcats show me that it allows SU permissions but no errors.

The code that it is not working is this one below.

File file = new File(Environment.getRootDirectory().getPath().toString(), "/etc/init.d/script");

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hasta.cocoremanager"
android:versionCode="1"
android:versionName="1.7" >

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">       
    <activity
        android:name="com.hasta.cocoremanager.Main"
        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>

Since 4.4.2 there are new rules for IO on the device storage:
Article

To put it short: Now you can only write in your app's folder.

Ensure that the file has read/write permissions to everyone.

Runtime.getRuntime().exec(new String[] { "su", "-c", "chown 777 /etc/init.d/script" });

Now you will be able to get an instance of that file.

If the file does not exist, first run

Runtime.getRuntime().exec(new String[] { "su", "-c", "echo '#' > /etc/init.d/script" });

EDIT: Without knowing how you want to use the file instance, i'll take a stab at it

File file = null;
try
{
    file = new File(Environment.getRootDirectory().getPath().toString(), "/etc/init.d/script");
}
catch (Exception e)
{
    // Cannot get file so set permissions.
    Runtime.getRuntime().exec(new String[] { "su", "-c", "chown 777 /etc/init.d/script" });
    try
    {
        file = new File(Environment.getRootDirectory().getPath().toString(), "/etc/init.d/script");
    }
    catch (Exception e)
    {
        // File does not exits, so create it.
        Runtime.getRuntime().exec(new String[] { "su", "-c", "echo '#' > /etc/init.d/script" });
        // Now set permissions.
        Runtime.getRuntime().exec(new String[] { "su", "-c", "chown 777 /etc/init.d/script" });
        try
        {
            file = new File(Environment.getRootDirectory().getPath().toString(), "/etc/init.d/script");
        }
        catch (Exception e)
        {
             // Should never get here.
        }    
}

// Do stuff with your file.

You can always do all this in your app user area and then do

Runtime.getRuntime().exec(new String[] { "su", "-c", "cp " + Environment.getDataDirectory() + "/script /etc/init.d/script" });

A note on calling su, this syntax also works.

Runtime.getRuntime().exec("su -c cp " + Environment.getDataDirectory() + "/script /etc/init.d/script");

Set menifest permissions that both:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Becasuse also you required the read permission for RW and READ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM