0

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

MMG
  • 21
  • 1
  • 9

1 Answers1

0

You can use the assets folder and put it there. To access the File use it like this

 InputStream is = getResources().getAssets().open("yourFile.xml");
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • With that I receive an `InputStream` object and I'm using a File object in my method: `public static Metro miMetro = parser.parsearMetro(xml);` And also I cannot use static reference and I would like to use this Metro object that those lines create in other classes. – MMG Dec 14 '16 at 15:55
  • @MiguelMG In that case you have to create a new `File` in sdcard path e.g. [something like this](http://stackoverflow.com/questions/11820142/how-to-pass-a-file-path-which-is-in-assets-folder-to-filestring-path) – Murat Karagöz Dec 14 '16 at 15:57
  • But this is not gonna be in `.apk` isn't? – MMG Dec 14 '16 at 15:59
  • @MiguelMG It will be in the apk. You will basically create a new File with the InputStream so you have a `FileObject`. Besides that your `Parser` probably uses an Inputstream anyways. – Murat Karagöz Dec 14 '16 at 16:00
  • After this I get an error, I have edited the question – MMG Dec 14 '16 at 16:35