0

谁能告诉我如何将文件从我的应用程序资产文件夹复制到 /system 文件夹?我知道如何获得 root 访问权限等等。例如:我想从“/assets/lib/libs.so”复制文件并检查该文件是否已经存在,如果它确实将其替换为新的“/system/lib/libs.so”。

4

2 回答 2

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 ) {
}
于 2012-09-01T08:19:55.200 回答
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"/>
于 2012-09-01T08:28:07.390 回答