Files directory in android - android

I have a file into "files" directory in my android project in eclipse, but when run the application in my device or in a device emulator, i don't find this directory
i want to put this file in /data/data/MYPACKAGE/files/mifile.txt during the installation

put it under assets folder and use getAssets() to access it
InputStream inputStream = getAssets().open("files/myfile.txt");
this will return the InputStream from the file then you can read it.

You could use getRessourceAsStream(String path) or simply use the assets folder and getAssets()

Related

Read asset file from an Android module

I would like to know how to access the assets directory from android (file path)
I tried something like :
Uri.parse("file:///android_asset/Resources/sound/" + sound);
But it doesn't work !
I know that I could do that easily with Titanium BUT I need the URI file from the JAVA module ...
Any idea ?
No Uri or File class.
You have to use the assets manager and then open an inputstream for your file and read from the stream.

Put an existing file to data\data\app_name\files during .apk installaion

I need to work with a file,that should be at data\data\app_name\files\ . That is now an SQL Database, so solution with SQLiteOpenHelper isn't ok for me. Also, I copyied that file to app_name\app\assets in my profect folder - that didn't work.
How to add that file to installation .apk, to put it in data\data\app_name\files during the installation?
Save your file in \res\raw within your project.
Then, you can access that file using:
InputStream databaseInputStream = getResources().openRawResource(R.raw.yourfile);
Make sure that the resource files are included correctly when generating the apk.

Android unity3d image path

I am trying to get an instance of an image in Unity that is in:
/assets/texture/image.png
Here is what I did:
path = Application.persistentDataPath;
path2 = Application.dataPath;
path3 = Application.streamingAssetsPath;
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance("jar:file://" + path2 + "!/assets/texture/image.png");
I did try with all the paths, and I am getting:
NotSupportedException:
jar:file:/data/app/com.myapk!/assets/texture/image.png.
Any help?
To access a file in the assets folder of your apk:
String path = getAssets() + "/texture/image.png";
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(path);
The path should be something like:
file:///android_asset/texture/image.png
EDIT: Can't use getAssets in Unity - Then there will not be a direct system-mounted file path for files in the assets folder.
What I usually do for C++/NDK applications with graphical assets is to decompress the assets directory to the SDCard to have a direct path access. On first launch of the app, you extract your assets folder to /mnt/sdcard (getExternalStorageDirectory) then use that. Otherwise I have no Idea how to do it in Unity directly, but there must be something.
If you want to make this easy on yourself, put your texture in the Resources/ folder and use Resources.Load to load it. Or put it in StreamingAssets/ and use the WWW class to load if from the jar file.

Can't locate file context.getDir("data", ...) in Android

I have a db located at
ctx.getApplicationContext().getDir("data", 0) + "/" + "db.db4o";
How can I browse this file with an file manager or via USB? I can't seem to find it :<
Browse with file manager, or USB, what does that mean? You can access the data directory, through a file manager, only if you are on a rooted device or an emulator.
Your application specific file are not directly stored in the data directory, but
data/data/your_package_name
you could create an "asset" folder into your android project and put it there. Before that you can try with this:
InputStream is = getAssets().open("db.db4o");
And convert "is" if is necessary.
Regards

Bad path trying to open file in Android

I'm trying to open a file with this:
document = builder.parse(new File("Data.xml"));
and I'm getting this message:
/Data.xml: open failed: ENOENT (No such file or directory)
and the file is in the root directory of the android project.
You are trying to open a file located in / (in linux this is the root directory of your file system). Instead you should be trying to create a file either on the SDCard or within the local storage directory for your application.
See this for more clarification: http://developer.android.com/guide/topics/data/data-storage.html
Move Data.xml into the assets folder of your project. Then to get a file reference, call getResources().getAssets().openFd( "Data.xml" )
You should probably try using a file input stream constructor for the builder instead, and use openFileInput( String fileName ) to get that, which does only use your app's data directory.
Using persistent storage
openFileInput()

Categories

Resources