When creating an Android application, I have some files that needs to be stored on the android itself.
How do I do this?
If you have local files, like some error tones, some openning video.. then place it in you assets folder of your project.
If you have dynamic data need to download at run time then use this guide.
Best place for generic files would be the assets folder.
You can access files through the AssetManager, which you can get with Activity.getAssets() for example.
Here is an example how you could access a text file:
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(getAssets().open("sometextfile.txt")));
// do stuff with br
} catch (IOException e) {
e.printStackTrace();
}
For more information on AssetManager read the Java Doc. Oh and yes, you can create folders in assets.
If you want to keep some files like readme.txt or even music files, you can use the raw folder inside of res folder. So create a folder named raw inside of res folder.
Inside of raw folder, let us assume that there is a file named readme.txt, assuming that the Activity class is called MyActivity.
Now, you can read the contents of a file into a String as shown below:
StringBuilder strContents = new StringBuilder();
String thisLine;
InputStream is = MyActivity.this.getResources().openRawResource(R.raw.readme);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((thisLine = br.readLine()) != null) { // while loop begins here
strContents.append(thisLine);
}
br.close();
//Now you have the data in strContents
Alternately, assets is also one such folder that you can use since the raw folder contains the file as is without any optimization, zipping done by Android.
So create an assets folder in your Project root folder and place your files there e.g. myfile.
Now, you can get an instance of the file InputStream as given below:
InputStream is = getBaseContext().getAssets().open("mydb");
Related
I am using xamarin forms and for android I would like to fetch local web content (not store it in android assets) so that it can be updated independently of the app itself.
What folder should be used as I know that WebView is running as separate process so not all folders are accessible?
Is there any doc describing what folders WebView can reach?
An easy way is to add the html in raw folder for the Resource folder. Android provideds Resources.OpenRawResource to read the stream from the file.
Create a raw folder inside the Resource folder. And add the local html file in it.
The code sample below for your reference:
SetContentView(Resource.Layout.layout4);
var stream = Resources.OpenRawResource(Resource.Raw.local);//local is the html file.(local.html)
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
var text = new System.Text.StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
text.AppendLine(line);
}
var webView1 = FindViewById<WebView>(Resource.Id.webView1);
webView1.LoadData(text.ToString(), "text/html", "UTF-8");
This question already has answers here:
How to get FileInputStream to File in assets folder
(2 answers)
Closed 7 years ago.
I know that I can read files from assets using AssetManager, like here
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(filename)
but open() method returns InputStream. So what I should do when I need to work with FileInputStream not with it superclass. Is there a way to get FileInputStream instance by InputStream instance?
No, there is not a way to convert an arbitrary InputStream back to a FileInputStream, as the data source may be of a different nature - something other than a literal File.
Assets are not files on the device, but rather particular chunks of the zip file which is your .apk. The Asset APIs give you access that is file-like in many ways (particularly with regard to input streams), but does not ultimately wrap an individual java.io.File, but rather an engine for extracting data directly from the .apk
In that case you can create a StringBuilder and read whole content of text file in it something like this
StringBuilder buf=new StringBuilder();
InputStream is=getAssets().open(filename);
BufferedReader in= new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
Note: Things differ based on the file type you try to access; as Chris Stratton wrote as the data source may be of a different nature.
I need to write an application that modify a "PDF template" file that contain some form inside.
My application should make a copy of that template, fill form inside and save it with an arbitrary name (keeping the original template for other tasks).How can I add that file into my project, and retrieve it inside my code?
Usually such files are put in the asset folder of your project.
Then you can access the file by using
getAssets().open("filename");
in the case of opening a text file with a stream reader use
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
I don't know the 'protocol' required to open/edit pdf files.
I have seen many question about accessing files in assets folder but can't seem to get a solid answer. I'm working on a application that would extract a text from PDF file thus I'm using iText Library to do that but my problem here is the file pathing of PDF.
I have tried using assetManager and assetManager opens that file but i think iText needs the file path so it can open the file by itself, i am not sure but that is my theory.
PdfReader reader = new PdfReader(<String PDF-file>);
then How do I access the file under assets folder using iText? if it is not possible is there a way to do that?
Here is sample code to get asset directory for file and use it for pdfreader.
code:
File f = FileUtils.fileFromAsset(this, "filename.pdf");
PdfReader pdfReader = new PdfReader(f.getPath());
You can get an InputStream object like this
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
and then use this constructor
public PdfReader(InputStream is)
throws IOException
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#PdfReader(java.io.InputStream)
I have solved my problem my using this:
reader = new PdfReader(getResources().openRawResource(R.raw.resume));
I need to use a file of type .dat in Android, but I cannot reference to the file ,application doesnt realize that file
Is that code correct?
File sdcard=Environment.getExternalStorageDirectory();
File file=new File(sdcard,"dictionary.dat");
if(file.exists()){
Toast.makeText(context,"file exists",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(context,"NOT exists",Toast.LENGTH_LONG).show();
}
String path=file.getAbsolutePath();
FileInputStream fis=new FileInputStream(path);
DataInputStream dawgDataFile=new DataInputStream(fis);
maybe I coded in wrong way, I just need to use "dictionary.dat" file , and I have stored it in raw folder. Button application says "NOT exists" . help please
you cannot get a file saved in RAW folder like that. This is how you can do it:
InputStream inputStream = getResources().openRawResource(R.raw.dictionary);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
//then start reading lines from reader with reader.readLine()
P.S. when saving files in RAW folder, file extensions can be omitted as they are not available when referencing in code