Created
December 5, 2011 22:09
-
-
Save fpersson/1435608 to your computer and use it in GitHub Desktop.
Android: Copy raw resources to external storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package photo.mission; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import android.content.Context; | |
import android.os.Environment; | |
import android.util.Log; | |
/** | |
* @author fredrik.persson | |
*/ | |
public class Setup extends Thread { | |
public Setup(Context context){ | |
m_context = context; | |
m_sdcard = Environment.getExternalStorageDirectory(); | |
} | |
public void run(){ | |
File cfgdir = new File(m_sdcard+m_configdir); | |
if(!cfgdir.exists()){ | |
cfgdir.mkdirs(); | |
} | |
copyResources(R.raw.foo); | |
copyResources(R.raw.bar); | |
copyResources(R.raw.baz); | |
} | |
public void copyResources(int resId){ | |
Log.i("Test", "Setup::copyResources"); | |
InputStream in = m_context.getResources().openRawResource(resId); | |
String filename = m_context.getResources().getResourceEntryName(resId); | |
File f = new File(filename); | |
if(!f.exists()){ | |
try { | |
OutputStream out = new FileOutputStream(new File(m_sdcard+m_configdir, filename)); | |
byte[] buffer = new byte[1024]; | |
int len; | |
while((len = in.read(buffer, 0, buffer.length)) != -1){ | |
out.write(buffer, 0, len); | |
} | |
in.close(); | |
out.close(); | |
} catch (FileNotFoundException e) { | |
Log.i("Test", "Setup::copyResources - "+e.getMessage()); | |
} catch (IOException e) { | |
Log.i("Test", "Setup::copyResources - "+e.getMessage()); | |
} | |
} | |
} | |
Context m_context; | |
File m_sdcard; | |
String m_configdir = "/config"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks mate. This helped me a lot.