chmod 失败:android 中的 EPERM(不允许操作)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24823861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:49:00  来源:igfitidea点击:

chmod failed: EPERM (Operation not permitted) in android?

androidandroid-sqlite

提问by Mahesh

i want to create database in sdcard or external sdcard for this i have try this code and using this i have successfully created database in sdcard but in logcat it give me warning like below

我想为此在 sdcard 或外部 sdcard 中创建数据库我尝试了这段代码并使用它我已经成功地在 sdcard 中创建了数据库但是在 logcat 中它给了我如下警告

Logcat

逻辑猫

07-18 14:18:22.140: W/FileUtils(8595): Failed to chmod(/mnt/sdcard/peakmedia/DB_PMD): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted)

07-18 14:18:22.140:W/FileUtils(8595):chmod(/mnt/sdcard/peakmedia/DB_PMD)失败:libcore.io.ErrnoException:chmod 失败:EPERM(不允许操作)

DB_Helper.java

DB_Helper.java

public class DB_Helper extends SQLiteOpenHelper
{

    public DB_Helper(Context context) 
    {   
        super(context, Environment.getExternalStorageDirectory().getAbsolutePath() 
                       + File.separator + DB_Constant.DB.FILE_DIR 
                       + File.separator + DB_Constant.DB.DATABASENAME, null, DB_Constant.DB.DB_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        String  query=String.format(DB_Constant.CREATE_TABLE_QUERY.CREATE_MYFILES_TABLE);
        db.execSQL(query);


        query=String.format(DB_Constant.CREATE_TABLE_QUERY.CREATE_MYUSERS_TABLE);
        db.execSQL(query);

        query=String.format(DB_Constant.CREATE_TABLE_QUERY.CREATE_MYPLAYLIST_TABLE);
        db.execSQL(query);

        query=String.format(DB_Constant.CREATE_TABLE_QUERY.CREATE_MYDEVICE_TABLE);
        db.execSQL(query);

    }

     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     {
            if(newVersion > oldVersion)
            {   

                db.execSQL("DROP TABLE IF EXISTS "+ DB_Constant.TABLE.MYFILES);
                onCreate(db);

                db.execSQL("DROP TABLE IF EXISTS "+ DB_Constant.TABLE.MYUSERS);
                onCreate(db);

                db.execSQL("DROP TABLE IF EXISTS "+ DB_Constant.TABLE.MYPLAYLIST);
                onCreate(db);

                db.execSQL("DROP TABLE IF EXISTS "+ DB_Constant.TABLE.MYDEVICE);
                onCreate(db);

            }
     }
}

回答by Chandler

Just solved this problem.

刚刚解决了这个问题。

You have to let your app join linux build to grant it SYSTEM permission.

您必须让您的应用程序加入 linux build 以授予它 SYSTEM 权限。

  1. add this line into Android.mk

    LOCAL_CERTIFICATE := platform

  2. add this into manifest node of AndroidManifest.xml

    android:sharedUserId="android.uid.system"

  3. Generate apk and push it into /system/app/

  4. Now you can try to run

    final String command = "chmod 777 /data/ena";
    Process p = Runtime.getRuntime().exec(command);
    

    or

    File file = new File("/data/ena");
    if (file.exists()) {
        boolean result = file.setExecutable(true);
        Log.e(TAG, "trpb67, RESULT IS " + result);
    }
    

    value of result should be true

  1. 将此行添加到 Android.mk

    LOCAL_CERTIFICATE := platform

  2. 将此添加到的清单节点 AndroidManifest.xml

    android:sharedUserId="android.uid.system"

  3. 生成apk并推送到/system/app/

  4. 现在你可以尝试运行

    final String command = "chmod 777 /data/ena";
    Process p = Runtime.getRuntime().exec(command);
    

    或者

    File file = new File("/data/ena");
    if (file.exists()) {
        boolean result = file.setExecutable(true);
        Log.e(TAG, "trpb67, RESULT IS " + result);
    }
    

    结果的值应该是真的

回答by LincyTonita

Check whether your device is connected in MTP usb mode. Sometimes a previous connection of MTP mode can cause this problem. Just restart device and check if it works.

检查您的设备是否以 MTP USB 模式连接。有时先前连接的 MTP 模式会导致此问题。只需重新启动设备并检查它是否有效。

回答by Hymanie_Hung

Yesterday I met same problem like Mahesh(appear same warn message, insert data from SQL server to SQLite, 2 tables should receive data in SQLite, but only 1 table received data, another table is empty).

昨天我遇到了和 Mahesh 一样的问题(出现同样的警告信息,从 SQL 服务器插入数据到 SQLite,2 个表应该在 SQLite 中接收数据,但只有 1 个表接收到数据,另一个表是空的)。

But Today I try to insert data from SQL server into 2 tables of SQLite in SD Card again, this time, 2 table received data.

但是今天我再次尝试将SQL服务器中的数据插入SD卡中SQLite的2个表中,这次是2个表接收到数据。

The only difference is EMULATOR setting.

唯一的区别是 EMULATOR 设置。

Enlarge SD card space setting, from few MB to 8GB, reference picture.emulator setting photo

放大SD卡空间设置,从几MB到8GB,参考图片。模拟器设置照片

Although warning also appear, but inserting work success.

虽然警告也出现了,但是插入工作成功了。

回答by mapodev

You have to change the owner (chown) of this folder. Currently the app/you is not the owner of that folder in where you want to install the db.

您必须更改此文件夹的所有者(chown)。目前,应用程序/您不是要安装数据库的文件夹的所有者。

So you have to do something like

所以你必须做类似的事情

sudo chown -R theuser /mnt/sdcard/peakmedia/DB_PMD