File copy and replacing in /system directory with root permission?

丶灬走出姿态 提交于 2019-12-08 10:36:02

问题


Can anyone kind enough show me how to copy files from my app assets folder to /system folder? I know how to get root access and all. For example: I want to copy file from "/assets/lib/libs.so" and check if this file already exist, if it does replace it to new "/system/lib/libs.so".


回答1:


try this:

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



回答2:


This will check if your file exists, delete it and then copy everthing that is in assets.

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

Edit:

Declare this permission in your manifest file for filesystems.

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


来源:https://stackoverflow.com/questions/12226053/file-copy-and-replacing-in-system-directory-with-root-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!