To create an Android file from a resource or a raw file, I do this:
try{ InputStream inputStream = getResources().openRawResource(R.raw.some_file); File tempFile = File.createTempFile("pre", "suf"); copyFile(inputStream, new FileOutputStream(tempFile)); // Now some_file is tempFile .. do what you like } catch (IOException e) { throw new RuntimeException("Can't create temp file ", e); } private void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); } }
- Remember to close your threads, etc.
source share