1

I'm trying code an Android app that would parse a .owl file (OWL file) and display the classes and subclasses. I keep getting the following exception when ever I try open the File.

02-22 18:48:40.421: W/System.err(535): java.io.FileNotFoundException: /Antibiotics.owl: open failed: ENOENT (No such file or directory)
02-22 18:48:40.431: W/System.err(535):  at libcore.io.IoBridge.open(IoBridge.java:406)
02-22 18:48:40.431: W/System.err(535):  at java.io.FileInputStream.<init>(FileInputStream.java:78)
02-22 18:48:40.431: W/System.err(535):  at com.Sample.SampleApp.StartingPoint.parseOWLFile(StartingPoint.java:102)
02-22 18:48:40.431: W/System.err(535):  at com.Sample.SampleApp.StartingPoint$3.onClick(StartingPoint.java:66)

Here is my Android code:

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.os.Bundle;

public class StartingPoint extends ActionBarActivity {

private static final int REQUEST_PATH = 1;
String currentFile;
EditText et1;
Button browse, ok, exit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText)findViewById(R.id.editText);

    browse = (Button) findViewById(R.id.browseButton);
    browse.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            getfile(v);
        }

    });

    exit = (Button)findViewById(R.id.ExitButton);
    exit.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        }

    });

    ok = (Button)findViewById(R.id.OKButton);
    ok.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v)
        {
            parseOWLFile();
        }

    });
}

public void getfile(View view)
{
    Intent i1 = new Intent(this, FileChooser.class);
    startActivityForResult(i1, REQUEST_PATH);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == REQUEST_PATH)
    {
        if(resultCode == RESULT_OK)
        {
            currentFile = data.getStringExtra("GetFileName");
            et1.setText(currentFile);
        }
    }
}

public void parseOWLFile()
{
    //String file = et1.getText().toString(); //Uncomment this when using on phone/tablet

    String file = "Antibiotics.owl";

    try
    {
        //System.out.println("File name is "+file);

        File f = new File(file);
        System.out.println("File name is "+f.toString());
        FileInputStream fis = new FileInputStream(f);

        OntModel base = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        base.read(fis, null);

        String ns = base.getNsPrefixURI("Ontology");

        Resource r = base.getResource(ns+" Concept");
        OntClass theClass = r.as(OntClass.class);

        Individual indiv = base.createIndividual(ns+"IndivTest", theClass);

        for (Iterator<Resource> i = indiv.listRDFTypes(true); i.hasNext(); )
            System.out.println( indiv.getURI() + " is asserted in class " + i.next() );

        OntModel inf = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF, base );

        Individual ind = inf.getIndividual( ns + "indivTest" );
        for (Iterator<Resource> i = ind.listRDFTypes(true); i.hasNext(); )
            System.out.println( ind.getURI() + " is inferred to be in class " + i.next() );



    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

}

I have the file "Antibiotics.owl" in the parent directory of the Project. I'm running the Project on an emulator.

How can I get rid of this exception and parse the OWL file? I'm still a beginner in Android programming.

halfer
  • 19,824
  • 17
  • 99
  • 186
user2201650
  • 527
  • 7
  • 13
  • 28
  • Is there an /Antibiotics.owl file on your phone? – user253751 Feb 23 '15 at 00:06
  • @immibis No. There isn't. Im running this project on the emulator. – user2201650 Feb 23 '15 at 00:08
  • 1
    then, what is really your question? – Marcin Orlowski Feb 23 '15 at 00:11
  • Is there an /Antibiotics.owl file on the emulated phone? – user253751 Feb 23 '15 at 00:12
  • @immibis No. How do I put it on the emulated phone? – user2201650 Feb 23 '15 at 00:18
  • There is most definitely not any file belonging to your app in the root directory of any normal Android install. The appropriate places to store runtime-modifiable data are in your app's private storage or in the external storage. Please see the SDK docs on data storage. Additionally there, are other ways to store initialized data in an APK. Finally, you have the option of fetching data from a server after installation, using either your own mechanism or the obb one. – Chris Stratton Feb 23 '15 at 01:00
  • Delete the cache and then uninstall the app and then restart the phone that worked for me – Sagar Devanga Jul 16 '15 at 14:26

1 Answers1

3

You must put the file into the assets folder or in the raw folder and then you can acces to the file. You can read more of the Android Application Modules in this link. and here you can find how to read a file located into raw folder.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
f3rn4nd000
  • 434
  • 3
  • 7
  • I've put the file in the **assets** folder of the Eclipse project. But I still keep getting the same exception. – user2201650 Feb 23 '15 at 00:20
  • In your parseOWLFile method you hardcode the path of the file here: String file = "Antibiotics.owl"; you need change this for: FileInputStream fis = getAssets().open("Antibiotics.owl"); – f3rn4nd000 Feb 23 '15 at 00:24
  • 1
    The contents of Assets and raw Resources, while accessible, are not "files" at runtime. – Chris Stratton Feb 23 '15 at 00:59
  • @f3rn4nd000 I put the file in the **assets** folder as you said. But when I run the application on the emulator, it crashes with the following exception: **java.lang.ExceptionInInitializerError** at **parseOWLFile** and **StartingPoint$3.onClick**. How can I fix this issue? Can you please help me? – user2201650 Feb 23 '15 at 01:32