Programatically save apk file from asset folder to

2019-06-14 16:32发布

I am trying to programatically save apk file from asset folder to system/app folder in android.

I am using following code. but error is showing read only file system.

AssetManager assetManager = getAssets();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open("youtuberanjit.apk");
        out = new FileOutputStream("/system/app/youtuberanjit.apk");
        byte[] buffer = new byte[1024];
        int read;
        while((read = in.read(buffer)) != -1){
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.fromFile(new File("/system/app/youtuberanjit.apk")), "application/vnd.android.package-archive");
        startActivity(intent);
    }catch(Exception e){
        // deal with copying problem
        Log.e("ERRORR", ""+e.getMessage());
    } 

Please help me.

Thank you!

1条回答
冷血范
2楼-- · 2019-06-14 16:56

You cannot write to /system unless you have rights to do that. You will need a rooted device for that.

Why do you want it to be a system app, and not a 'normal' user app?

查看更多
登录 后发表回答