Skip to content Skip to sidebar Skip to footer

Copying Xml File From Res/xml Folder To Device Storage

I'm trying to copy an xml file from the res/xml folder to the device storage but I'm really struggling on how to do this. I know that the starting point is to get an InputStream to

Solution 1:

From res/xml you can't you have to put all files in your assets folder then use below code

Resourcesr= getResources();
AssetManagerassetManager= r.getAssets();

Filef=newFile(Environment.getExternalStorageDirectory(), "dummy.xml");
InputStreamis= = assetManager.open("fileinAssestFolder.xml");
OutputStreamos=newFileOutputStream(f, true);

finalintbuffer_size=1024 * 1024;
try
{
    byte[] bytes = newbyte[buffer_size];
    for (;;)
    {
        intcount= is.read(bytes, 0, buffer_size);
        if (count == -1)
            break;
        os.write(bytes, 0, count);
    }
    is.close();
    os.close();
}
catch (Exception ex)
{
    ex.printStackTrace();
}

Solution 2:

I think you should use the raw folder instead. Have a look at http://developer.android.com/guide/topics/resources/providing-resources.html.

Solution 3:

You can also use this code:

   try {
        InputStream input = getResources().openRawResource(R.raw.XZY);
        OutputStream output = getApplicationContext().openFileOutput("xyz.mp3", Context.MODE_PRIVATE);
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }

And when you need file use this code:

Filek=getApplicationContext().getFileStreamPath("xyz.mp3");

Post a Comment for "Copying Xml File From Res/xml Folder To Device Storage"