Reputation: 4321
The file model.obj is in the assets directory of my project. The toast that comes out is a File Not Found exception. I am running the program on my Galaxy S3 not a virtual device. Do I have to specify some path to the file?
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.content.Context;
import android.app.Activity;
public class ImportOBJ {
protected void onCreate(String filename,Context context)
{
try
{
FileInputStream fis = context.openFileInput(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = null, input="";
while( (line = reader.readLine()) != null )
{
input += line;
}
reader.close();
fis.close();
}
catch (Exception ex)
{
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
}
}
}
ManActivity:
package com.example.tictactoeshowgrid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImportOBJ obj_import=new ImportOBJ();
obj_import.onCreate("model.obj",MainActivity.this);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 0
Views: 671
Reputation: 6434
Use the following code to get the file from asset manager:
AssetManager assetManager = getResources().getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("foo.txt");
if ( inputStream != null)
Log.d(TAG, "It worked!");
} catch (IOException e) {
e.printStackTrace();
}
}
Upvotes: 2
Reputation: 2374
the path is assets/file/model.obj, you can read file like this
InputStream fis = mContext.getAssets().open("file/model.obj");
Upvotes: 1
Reputation: 5373
FileInputStream fis = context.openFileInput(filename);
Can be replaced with
InputStream fis = context.getAssets().open(filename);
GetAssets()
returns an AssetManager:
Provides access to an application's raw asset files; see Resources for the way most applications will want to retrieve their resource data. This class presents a lower-level API that allows you to open and read raw files that have been bundled with the application as a simple stream of bytes.
Upvotes: 1