开发者

Moving asset to /data/data/APP_NAME/files

开发者 https://www.devze.com 2023-03-01 21:57 出处:网络
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

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 "开发者_JS百科Files even show up.


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();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消