从withing一个APK到内部存储器复制文件 [英] Copying a file from withing a apk to the internal storage

查看:110
本文介绍了从withing一个APK到内部存储器复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从APK推一个文本文件到/ system目录(是其根用户的应用程序),并想知道我怎么会做这样:)我的txt文件是在资产产生的文件夹,但它可能是使用,如果需要的

I would like to push a text document from the apk into the /system directory (Yes its a app for rooted users) and was wondering how i would do this :) My txt file is in the assests folder but it could be used if needed

推荐答案

将在项目的资产目录下的文本文件,然后将其解压到与code像在以下螺纹的文件系统: <一href="http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard">Android:如何在资产到SD卡?

Place the text file in your project's assets directory and then extract it to the filesystem with code like the one in the following thread: Android: How to copy files in 'assets' to sdcard?

编辑:这是我用一些code。对于sourceFileName,传之名的文件的资产相对于资产的文件夹(例如,如果你有MYFILE.TXT的资产文件夹,通​​过MYFILE.TXT)。对于目标文件,通过一个完整路径(如/data/data/com.mycompany/mypackage/myFile.txt)。上下文是当前活动(例如MyActivity.this)。

Here is some code that I use. For sourceFileName, pass in the name of the assets file relative to the assets folder (e.g. if you have myFile.txt in the assets folder, pass myFile.txt). For the destination file, pass a full path (e.g. /data/data/com.mycompany/mypackage/myFile.txt). context is the current activity (e.g. MyActivity.this).

private boolean copyFile(Context context, String sourceFileName, String destFileName)
{
    AssetManager assetManager = context.getAssets();

    File destFile = new File(destFileName);

    File destParentDir = destFile.getParentFile();
    destParentDir.mkdir();

    InputStream in = null;
    OutputStream out = null;
    try
    {
        in = assetManager.open(sourceFileName);
        out = new FileOutputStream(destFile);

        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;

        return true;
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return false;
}

EDIT2:原来的/系统分区挂载为只读,甚至在根设备。这可以帮助:<一href="http://stackoverflow.com/questions/5481395/android-how-to-mount-filesystem-in-rw-from-within-my-apk-rooted-of-course">Android:如何挂载文件系统中RW从我的APK内? (扎根,当然)

这篇关于从withing一个APK到内部存储器复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆