创建文件到系统的Andr​​oid 4.4.2文件、系统、Andr、oid

由网友(扬帆起航℃)分享简介:我不知道为什么,因为我切换到奇巧我的应用程序不能创建在系统中的任何文件(此方法是工作在Android 4.1.2)。我的应用程序有苏权限,坐骑系统在清单中声明RW阅读并WRITE_EXTERNAL_STORAG​​E权限。Logcats告诉我,这让苏权限,但没有错误。I don't know why since I...

我不知道为什么,因为我切换到奇巧我的应用程序不能创建在系统中的任何文件(此方法是工作在Android 4.1.2)。我的应用程序有苏权限,坐骑系统在清单中声明RW阅读并WRITE_EXTERNAL_STORAG​​E权限。 Logcats告诉我,这让苏权限,但没有错误。

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.

在code,它不工作是这个如下:

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

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

清单

<?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>

推荐答案

确保文件具有读/写权限的每一个人。

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" });

编辑:不知道你怎么想使用的文件实例,我会采取刺伤它

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 (Excpetion 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 (Excpetion 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 (Excpetion 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");
阅读全文

相关推荐

最新文章