File copy and replacing in /system directory with

2019-06-09 20:43发布

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".

2条回答
劫难
2楼-- · 2019-06-09 20:58

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"/>
查看更多
Rolldiameter
3楼-- · 2019-06-09 21:10

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 ) {
}
查看更多
登录 后发表回答