Getting File from /res/raw - android

I need your help, may be this question is very easy for you.
I have a file in /res/raw folder. For example, it's id is R.raw.myFile. I need to get "File" object. Such as i can do with file on sdcard
File file = new File("/sdcard/myFolder/myFile");
How I can do this?
By the way, sorry for my english if there are some mistakes.
I need "File" object. Not stream.

Use this
InputStream inputStream = getResources().openRawResource(R.raw.filename);

Related

Android : How to get actual URL path for XML stored under res/xml?

Hope the subject line makes it clear.
I want to get hold of the file path of the xml file which i store under the res/xml folder.
I'm aware of the InputStream approach by directly reading using the snippet.
... getResources().getXml(R.raw.testxml);
but my requirement is to just get the path of the stored file for the moment.
Any help appreciated
VATSAG
try as to get full path of file stored in res/raw folder
String fullpath = "android.resource://" +
YOUR_FULL_PACKAGE_NAME +
"/"+R.raw.testxml;

File not found issue in Android

I need to access myfile.txt file using FileReader in Android , please suggest me where to add the text file in Eclipse. I tried it adding it in Resource and Asset but I am getting File not found issue.
FileReader fr = new FileReader("myfile.txt");
Even
File ff = new File("myfile.txt");
File Supports only the below listed parameters
FileReader Supports only the below listed parameters
Note: I want solution for this issue , only with FileReader or File
The directory would be /res/raw/ this is where you put all your extra resources.
you can refer to it using getResources().openRawResource(resourceName)
and check here Android how to get access to raw resources that i put in res folder?
EDIT:
how can i edit the text files in assets folder in android
in short
the easiest way would be to copy the file to external directory then do your stuff there
link is here
Android: How to create a directory on the SD Card and copy files from /res/raw to it?
One thing to mention - prior to 2.3 the file size in the assets cannot exceed 1MB.
hope it helps abit
That's how I obtain my file from the SD card, perhaps this can be some use to you.
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File options = new File(getAppDirectory(), "portal.xml");
}
The getAppDirectory method used in the bit of code looks like this :
private String getAppDirectory() {
return new String(Environment.getExternalStorageDirectory().toString()
+ "/foldername/foldername/");
}
After this bit of code I also make sure the file exists and what not before I attempt to read from it.

How to read file that belongs to other app from external storage?

I want to read text file, that was written by other my app. It's saved ad "Android/data/MyPackageName/files/"
I use this code:
File file = new File("//Android//data//MyPackageName//files//", "filename.txt");
FileInputStream is = new FileInputStream(file);
but i get exception "no such file or directory"
I am sure that solution is pretty simple, but i can't find it yet.
Thank you for your help!
I don't think it is right to use the double backslash "//", one is enough. Also, the path should be "/mnt/sdcard/Android/data....". I am not sure the "/mnt/sdcard" is applicable on every device, so my suggestion is to use Environment.getExternalStorageDirectory to get the root dir on sd card.

How to read an xml file from R.java

I have an xml file inside the res folder under the xml forlder. I want to parse it with SAX. I know there are lots of example but I want to understand InputStream, InputSource, File deeply. I can reach the file writing R.xml.hippo (hippo is the name of the xml file).
But how can I give this resource as a stream?
I know when I write R.xml.hippo to anywhere it will be reference(int) but I want give it as a source. What is and how converting an int to starting point of stream?
You can read XML from assets folder as well as from res/raw folder.
To read from assets folder
AssetManager manager = getAssets();
InputStream inputStream = manager.open("your_xml.xml");
To read from raw folder
InputStream inputStream = getResources().openRawResource(R.raw.your_xml);
And parse this inputStream to your XML Parser.
You should place the file inside the raw folder. Then you can access it via Resources.openRawResource(). For more information refer to Android Developer's page.

android: Check the existence of a file in the application

I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.

Categories

Resources