将资产移动到 /data/data/APP_NAME/files

发布于 2024-11-04 09:18:31 字数 1661 浏览 1 评论 0原文

快速参考我所读过的内容 http://thedevelopersinfo.com/2009/11/17/using- Android 中的资产/

http://www.wiseandroid .com/post/2010/06/14/Android-Beginners-Intro-to-Resources-and-Assets.aspx

还有更多,但作为新手只能发帖2.

在我的应用程序中,有一个按钮,如果用户决定在 root 设备上执行此操作,则将重新启动到引导加载程序。我有一个名为“rebo​​ot”的重新启动二进制文件,它将允许命令运行,它位于 /assets/ 中。使用上面的方法,我似乎无法“重新启动”来移动甚至在我的 apk 的 /data/data/ 中创建目录“文件”。我的问题是,是否有更好的指南来指导我学习该学科,或者这些是最好的指南,但我只是头脑太笨,无法理解它。或者,如果您有其他一些示例代码,我可以通读并尝试理解,那就完美了。谢谢。

添加了我正在做的事情的示例

$ public static String moveReboot = "/data/data/com.DPE.MuchSuck/Files";
public static String reboot = "file:///android_asset/reboot";
public static Context myContext;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        moveFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
$ public void moveFile() throws IOException {
  AssetManager assetManager = getAssets();
  InputStream myInput = assetManager.open(reboot);
  String outFileName = moveReboot + reboot;
  OutputStream myOutput = new FileOutputStream(outFileName);
  byte[] buffer = new byte [1024];
  int length;
  while ((length = myInput.read(buffer))>0){
   myOutput.write(buffer, 0, length);

  }
  myOutput.flush();
  myOutput.close();
  myInput.close();

运行 apk 后,“文件”中没有显示任何内容,“文件甚至也没有显示。

Quick reference on what I have read
http://thedevelopersinfo.com/2009/11/17/using-assets-in-android/

http://www.wiseandroid.com/post/2010/06/14/Android-Beginners-Intro-to-Resources-and-Assets.aspx

There are more but as a newb can only post 2.

In my app there is a button the will a reboot into the bootloader if the user decides to do so on rooted devices. I have a reboot binary called "reboot" that will allow the commmand to run and it is in /assets/. Using the methods above I can not seem to get "reboot" to move or even create the directory "files" in /data/data/ of my apk. My question is, is there a better guide to school me in the subject or are these the best and I am just to thick headed to understand it. Or if you have some other sample codes I can read through and try and understand would be perfect. Thank you.

Added sample of what I am doing

$ public static String moveReboot = "/data/data/com.DPE.MuchSuck/Files";
public static String reboot = "file:///android_asset/reboot";
public static Context myContext;

@Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        moveFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
$ public void moveFile() throws IOException {
  AssetManager assetManager = getAssets();
  InputStream myInput = assetManager.open(reboot);
  String outFileName = moveReboot + reboot;
  OutputStream myOutput = new FileOutputStream(outFileName);
  byte[] buffer = new byte [1024];
  int length;
  while ((length = myInput.read(buffer))>0){
   myOutput.write(buffer, 0, length);

  }
  myOutput.flush();
  myOutput.close();
  myInput.close();

Upon running the apk nothing shows up in the "Files" nor does "Files even show up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

混浊又暗下来 2024-11-11 09:18:31

doug,

看起来您的“outFileName”将不包含一个好的文件名。
尝试使用 getExternalFilesDir() 获取 SDRAM 的路径。

请参阅: http://developer.android .com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29

编辑...

doug,这是我

private void copyAssetToSDRAM(String strFilename)
{
    try
    {
        //complete path to target file
        File fileTarget = new File(Environment.getExternalStorageDirectory(), strFilename);

        //data source stream
        AssetManager assetManager = ApplicationContext.getContext().getAssets();
        InputStream istr = assetManager.open(strFilename);

        //data destination stream
        //NOTE: at this point you'll get an exception if you don't have permission to access SDRAM ! (see manifest)
        OutputStream ostr = new FileOutputStream(fileTarget);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = istr.read(buffer))>0)
        {
            ostr.write(buffer, 0, length);
        }
        ostr.flush();
        ostr.close();
        istr.close();

    }
    catch(Exception e)
    {
        Toast.makeText(ApplicationContext.getContext(), "File-Copy Error: "+strFilename, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

使用它的实现,请执行以下操作:

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        {
            copyAssetToSDRAM("myfile.png");
        }
        else
        {
            Toast.makeText(ApplicationContext.getContext(), "Unable to copy images. NO SDRAM", Toast.LENGTH_LONG).show();
        }

doug,

it looks like your "outFileName" won't contain a good filename.
Try using getExternalFilesDir() to obtain a path to SDRAM.

see: http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir%28java.lang.String%29

EDIT...

doug, this is my implementation

private void copyAssetToSDRAM(String strFilename)
{
    try
    {
        //complete path to target file
        File fileTarget = new File(Environment.getExternalStorageDirectory(), strFilename);

        //data source stream
        AssetManager assetManager = ApplicationContext.getContext().getAssets();
        InputStream istr = assetManager.open(strFilename);

        //data destination stream
        //NOTE: at this point you'll get an exception if you don't have permission to access SDRAM ! (see manifest)
        OutputStream ostr = new FileOutputStream(fileTarget);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = istr.read(buffer))>0)
        {
            ostr.write(buffer, 0, length);
        }
        ostr.flush();
        ostr.close();
        istr.close();

    }
    catch(Exception e)
    {
        Toast.makeText(ApplicationContext.getContext(), "File-Copy Error: "+strFilename, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}

to use it, do this:

        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        {
            copyAssetToSDRAM("myfile.png");
        }
        else
        {
            Toast.makeText(ApplicationContext.getContext(), "Unable to copy images. NO SDRAM", Toast.LENGTH_LONG).show();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文