I have a file located at /res/introduced.xml. I know that I can access it in two ways:
1) the R.introduced resource
2) some absolute/relative URI
I'm trying to create a File object in order to pass it to a particular class. How do I do that?
This is what I ended up doing:
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);
}
}
some absolute/relative URI
Very few things in Android support that.
I'm trying to create a File object in order to pass it to a particular class. How do I do that?
You don't. A resource does not exist as a file on the filesystem of the Android device. Modify the class to not require a file, but instead take the resource ID, or an XmlResourceParser
.