open assets or raw file as File - android

Is there a way to open assets or raw resource as File ? I have method that needs open file as File but since i want store my file in assets or in raw folder i cannot access them.
I tried open it with file:///android_asset/filename.xml but received FileNotFound exception.
File file = new File("....");
x.load(file);

Is there a way to open assets or raw resource as File ?
No, sorry. They do not exist as files. You can only get an InputStream.

If you're willing to zip up the file first, you can do this:
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.texfilezip), dir, true);
File textFile = new File(dir, "textfile.txt");

Related

How can I write a file in res directory?

I want to store a .json file within the res directory. How should the path be defined?
FileWriter file = new FileWriter("path_here");
You can't! Resources are read-only at runtime.

How to get access to application classes.dex file

I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?
you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help

Load File on Android

I want load a file wav from sdcard in my android project but I don't succeed.
I'm using
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"/mnt/sdcard/.....wav");
but I get this error
java.io.FileNotFoundException: /mnt/sdcard/mnt/sdcard/......wav (No such file or directory)
Can you help me, please?
the first parameter is already the path - do not put /mnt/sdcard/ to the second parameter
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,".....wav");
Remove this /mnt/sdcard/ from the line It is already there in file
Replace this:
File file = new File(sdcard,"/mnt/sdcard/.....wav");
with this:
File file = new File(sdcard,".....wav");

Android: ZipFile, FileNotFoundException when use Assets Files

I have a problem.
I use this code to read the content of a zip file:
File file = new File(TogglesManager.EXTERNAL_STORAGE_THEMES_DIRECTORY+"filename.zip");
ZipFile zip = new ZipFile(file);
But when I try to use an assets I have a FileNotFoundException.
To read asset zip file I use this code:
File file = c.getFileStreamPath("assetsFile.zip");
ZipFile zip = new ZipFile(file);
The "file" is not null, because if I write file.getName(); I see the correct file name.
I don't want to use ZipInputStream class, but only ZipFile
You cannot access Assets using normal file operations. You will have to use
AssetManager assetManager = mContext.getAssets();
InputStream is = assetManager.open("assetsFile.zip");
But since ZipFile does not take InputStream as a parameter, you will have to copy asset file onto internal storage and then use File
Further AFAIK, apk is zipped, so storing a zipped file inside assets might not change storage used by much, so maybe you donot need to use zipfile inside assets.

Read CSV File in android app

Thanks in advance,
How to read csv file from sdcard.
and how to give the permission of read/writer for file in android application.
thanks again.
This might be helpful to you
File dir = Environment.getExternalStorageDirectory();
File f = new File(dir, "path/dir1/sdcard.ext");
I don't know about the permissions but the above will certainly allow you to read the file from the sdcard in the path directory.

Categories

Resources