There is no path as such, you can actually get the Uri
where the file is located within your /raw
directory:
Uri archivo = Uri.parse("android.resource://" + getPackageName() + "/raw/myFile");
and from this Uri
get File
:
File file = new File(archivo.getPath());
if(file.exists()){
Log.i("Archivo", "file existe! ");
}else{
Log.i("Archivo", "No existe! ");
}
You can also get the InputStream
you can get using openRawResource()
:
InputStream inputStream = getResources().openRawResource(R.raw.myFile);
When you get InputStream
you can use this method to create a file:
private void copyInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Ensure that the InputStreams are closed even if there's an exception.
try {
if ( out != null ) {
out.close();
}
in.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
In this way you can create a file from InputStream
InputStream inputStream = getResources().openRawResource(R.raw.myFile);
String pathFile = Environment.getExternalStorageDirectory()+ "/Android/data/nuevo_archivo.txt";
copyInputStreamToFile(arq, new File(pathFile));