I'm trying to use an xml File which I need to parse, but I only can use it if I copy manually this file into my emulator (API Level 24) on path: /mnt/sdcard
I need this file into .apk
of my App, so I'm trying to use it from /res folder of my project, because I understand that so will this file finally included in .apk
.
This is how I'm using this file right now from sdcard
:
public static File xml = new File("/mnt/sdcard/metro.xml");
public static Parser parser = new Parser();
public static Metro miMetro = parser.parsearMetro(xml);
But I would like to change the location of this file because I want it to be inside App.
If I try:
public static File xml = new File("/res/xml/metro.xml");
App says it cannot find this file.
This is how app permissions look like into AndroidManifest.xml
:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Does someone know how can I include this file into final .apk
without changing my methods and use it internally without copying my xml file in each device?
So now my Activity looks like this:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.miguel.metromadappcesible.code.Metro;
import com.miguel.metromadappcesible.code.Parser;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IndexActivity extends AppCompatActivity {
public static Parser parser = new Parser();
public static Metro miMetro;
public IndexActivity() throws IOException {
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
try {
this.crearFicheroXML(getAssets().open("metro.xml"));
} catch (IOException e) {
e.printStackTrace();
}
File xml = new File ("/mnt/sdcard/metroMadrid.xml");
miMetro = parser.parsearMetro(xml);
}
public void crearFicheroXML (InputStream inputStream){
OutputStream outputStream = null;
try {
// read this file into InputStream
inputStream = getResources().getAssets().open("metro.xml");
// write the inputStream to a FileOutputStream
outputStream =
new FileOutputStream(new File("/mnt/sdcard/metroMadrid.xml"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
// outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And I get this error from console:
Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference