创建文件到系统的Andr​​oid 4.4.2 [英] Create File into system android 4.4.2

查看:189
本文介绍了创建文件到系统的Andr​​oid 4.4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么,因为我切换到奇巧我的应用程序不能创建在系统中的任何文件(此方法是工作在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");

这篇关于创建文件到系统的Andr​​oid 4.4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆