I have kept some binary files in raw directory in Android application. I want to access them in the application and create a file from it at runtime. I am going to send this file over network.
How can I refer the raw resources? I have done following code
private File getFileByResourceId(int id, String fileName) throws IOException {
File file = new File(fileName);
InputStream ins = ctx.openRawResource(id);
log.debug(ins.toString());
log.debug(file.getAbsolutePath());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int size = 0;
// Read the entire resource into a local byte buffer.
byte[] buffer = new byte[1024];
while ((size = ins.read(buffer, 0, 1024)) >= 0) {
outputStream.write(buffer, 0, size);
}
ins.close();
buffer = outputStream.toByteArray();
FileOutputStream fos = new FileOutputStream(file);
fos.write(buffer);
fos.close();
return file;}
But when I try to access this file it gives me permission errors.
Can someone propose a solution?