使用root权限在/ system目录中复制和替换文件?

时间:2012-09-01 08:05:25

标签: android file replace copy root

任何人都可以告诉我如何将文件从我的app资源文件夹复制到/ system文件夹吗?我知道如何获得root访问权限。例如:我想从“/assets/lib/libs.so”复制文件并检查此文件是否已存在,是否确实将其替换为新的“/system/lib/libs.so”。

2 个答案:

答案 0 :(得分:0)

试试这个:

try {
    File from = new File( "/assets/lib/libs.so" );
    File to = new File( "/system/lib/libs.so" );
    if( from.exists() && to.exists() ) {
        FileInputStream is = new FileInputStream( from );
        FileOutputStream os = new FileOutputStream( to );
        FileChannel src = is.getChannel();
        FileChannel dst = os.getChannel();
        dst.transferFrom( src, 0, src.size() );
        src.close();
        dst.close();
        is.close();
        os.close();
    }
} catch( Exception e ) {
}

答案 1 :(得分:0)

这将检查您的文件是否存在,删除它,然后复制资产中的所有内容。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



   File exists = new File("/system/lib/libs.so");
   if(exists.exists()){       
   exists.delete();
   CopyAssets();
   }else{
       CopyAssets();
   }


}


private void CopyAssets() { 
    AssetManager assetManager = getAssets(); 
    String[] files = null; 
    try { 
        files = assetManager.list(""); 
    } catch (IOException e) { 
        Log.e("tag", e.getMessage()); 
    } 
    for(String filename : files) { 
        InputStream in = null; 
        OutputStream out = null; 
        try { 
          in = assetManager.open(filename); 
          out = new FileOutputStream("/system/lib/" + filename);
          copyFile(in, out); 
          in.close(); 
          in = null; 
          out.flush(); 
          out.close(); 
          out = null; 
        } catch(Exception e) { 
            Log.e("tag", e.getMessage()); 
        }        
    }

} 

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
      out.write(buffer, 0, read); 
    } 
}

编辑:

在文件系统的清单文件中声明此权限。

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