Android writing to a text file - android

I have a text file within my raw folder of my app that I intend to use as a simple way to save settings and then read them back when needed. I can read from this file with using the BufferedReader and what comes with it, but I've tried a few different ways to be able to write to this file and none seem to work.
It seems to me that the problem is I never actually get the file, and I assume this is simply because I don't exactly know how I am supposed to give it the correct directory and file name. I've tried all I could come up with, and I tend to get errors like "No such file or directory exists" or "Read-only file system".
This seems to be a very simple problem relating to me just giving the wrong information, so if anybody could point me in the right direction it would be much appreciated.
Thank you,
Raphy

For saving settings you should use SharedPreferences rather than coming up with a custom solution.
SharedPreferences documentation
Data Storage

I'm not entirely sure you can do that with stuff in the "raw" folder.
One approach would be to use the SharedPreference storage in the API. See in the datastorage section of the docs. It's perfect from what you describe neededing. Another approach would be to put the file on the SDCard and read and write it from there.

Related

Change mode of the getDefaultPreference file in Android

I am using a PreferenceActivity to save some user preferences.
I want to access the default Preference file from outside the app for debugging purposes.
How can I change the defaultSharedPreferences file from private to public mode?
And also what is the default name of that defaultSharedPreference file?
[EDIT]
One negative vote that means something is wrong with the question.
At least, do let me know whats wrong!
That would be less cruel and not discouraging for a newbie like me :)
In the DDMS (or its equivalent, in Android Studio), the path to your preference file is /data/data/your.app.name/shared_prefs/your.app.name_preferences.xml.
You can export it, modify it, and even reimport it (at your own risk!).
EDIT
The path is correct, the file name might vary.
Sorry, I wasn't pointing the xml file, while taking the screen shot.
But it's there.

Can I change a string in the strings resource file to one from array in an activity?

Been having a little bit of trouble with this. Googled about and I can't really find anything. Saw a few things about setting text but I don't think that affects the string resource file or at least I can't make it affect the resource file. Help please!
No, you can't modify apk at the runtime. If you don't own source code, you can pull apk file to the computer and decompile it by using APKTool, for example. If you are working with data you get from internet/user, it should be kept inside DBs, files, whatever. In other words in internal or external storage. Please take a look on storage options you can use http://developer.android.com/guide/topics/data/data-storage.html

Creating a file on an Android device

I am new to Android development using eclipse, although not new to software development in general.
For my first real project, I am trying to modify the example SoftKeyboard that is supplied with the SDK. I want to modify one of the keys to act as a function key, when followed by a single letter key it will enter a canned string - performing a macro function.
So far so good. I have the key and graphics modified, and found where to respond. I would like to put the canned strings in an editable Properties file stored where the keyboard can find them.
That is where I'm having trouble. It seems that I can't to create and save a file. I don't know if it's read/write permission problem, whether the keyboard (it runs as a service) is not allowed to create a file, or my code is just plain wrong.
Can someone help me out – point me in the right direction?
Thank you very much.
Barry.
If these are canned files that come with the APK you install to the device and only need to read (not write), you can place them in the assets folder of your project. Then use the resource manager to load them:
Resources resources = getResources();
InputStream moduleSearchTemplateIn = resources.getAssets().open("file/name/here.properties");
If you want to read/write files on the SD card, you'll need to add a permission to your manifest. Though, for this purpose, I'd probably prefer a SQLite table.

Efficient adding text to the and in android

There is one very nice feature in shell
for example
# "asd" > myfile.txt
puts "asd" in myfile.txt if the file exist first it is deleted then it is created and the content is put in the file.
but if
# "asd" >> myfile.txt
and if the file exist then the "asd" will be just added to the end of the file.
well I need some EFFICIENT algorithm that do exactly this.
I have very large text file and all I want to do is to write something to the end, but it must be very efficient I do not want to waste resources on stupid things like read the whole content concatenate and write...
I know the general concepts about files. One thing which is useful for your case is opening a file in append mode. If you open a file in append mode and trying to write data to it, it just append(adds at the end of file) the given data.
Try to search for similar functionality in android API. This is general feature of File System. In linux also we have familiar with this type of file operations.
I hope it may help you.
Is this what you are looking for.

read an image I just wrote in android

So, largely for debugging purposes, I want to be able to write an image at arbitrary points in my code, and look at it later. I figured this would be easiest if I just wrote a my bitmap to a file and read it back later, but I cannot seem to figure out where to find the file after I write it, or how to open an image that is not in res/drawable with a corresponding handle in R.
You can use openFileOutput() and openFileInput(). These pull up data streams that point to files in your app's directory, and are (as far as I know), the suggested way to handle files that your app makes.

Categories

Resources