文件拷贝和替换/与root权限的系统目录?(File copy and replacing in /

2019-09-22 01:32发布

任何人都可以还跟我展示了如何复制文件从我的应用程序Assets文件夹/系统文件夹? 我知道如何获得root权限和所有。 例如:我想从“/assets/lib/libs.so”复制文件,并检查该文件已经存在,如果这样做,更换新的“/system/lib/libs.so”。

Answer 1:

尝试这个:

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 ) {
}


Answer 2:

这将检查你的文件存在,将其删除,然后复制寄托都认为是资产。

@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"/>


文章来源: File copy and replacing in /system directory with root permission?