Asked  7 Months ago    Answers:  5   Viewed   41 times

I want to save a file to the internal storage by getting the text inputted from EditText. Then I want the same file to return the inputted text in String form and save it to another String which is to be used later.

Here's the code:

package com.omm.easybalancerecharge;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {

        final EditText num = (EditText) findViewById(;
        Button ch = (Button) findViewById(;
        TelephonyManager operator = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String opname = operator.getNetworkOperatorName();
        TextView status = (TextView) findViewById(;
        final EditText ID = (EditText) findViewById(;
        Button save = (Button) findViewById(;

        final String myID = ""; //When Reading The File Back, I Need To Store It In This String For Later Use

        save.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Get Text From EditText "ID" And Save It To Internal Memory
        if (opname.contentEquals("zain SA")) {
            status.setText("Your Network Is: " + opname);
        } else {
            status.setText("No Network");
        ch.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Read From The Saved File Here And Append It To String "myID"

                String hash = Uri.encode("#");
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:*141*" + /*Use The String With Data Retrieved Here*/ num.getText()
                        + hash));

I have included comments to help you further analyze my points as to where I want the operations to be done/variables to be used.



Hope this might be useful to you.

Write File:

private void writeToFile(String data,Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());

Read File:

private String readFromFile(Context context) {

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput("config.txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {

            ret = stringBuilder.toString();
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());

    return ret;
Tuesday, June 1, 2021
answered 7 Months ago

There is a limitations on opening compressed files in the assets folder. This is because uncompressed files can be directly memory mapped into the processes virtual address space, therefore avoiding needing the same amount of memory again for decompression.

Dealing with Asset Compression in Android Apps discusses some techniques in dealing with compressed files. You can trick aapt into not compressing the file by using an extension that is not compressed (e.g. mp3) or you can manually add them to the apk without compression instead of getting aapt to do the work.

Wednesday, June 2, 2021
answered 7 Months ago

I have tried this with and without the WRITE_INTERNAL_STORAGE permission.

There is no WRITE_INTERNAL_STORAGE permission in Android.

How do I create this file for writing?

You don't, except perhaps on a rooted device, if your app is running with superuser privileges. You are trying to write to the root of internal storage, which apps do not have access to.

Please use the version of the FileOutputStream constructor that takes a File object. Create that File object based off of some location that you can write to, such as:

  • getFilesDir() (called on your Activity or other Context)
  • getExternalFilesDir() (called on your Activity or other Context)

The latter will require WRITE_EXTERNAL_STORAGE as a permission.

Is there an easier way than writing it to a file then reading from it again?

You can temporarily put it in a static data member.

because many people don't have SD card slots

"SD card slots" are irrelevant, by and large. 99% of Android device users will have external storage -- the exception will be 4+ year old devices where the user removed their SD card. Devices manufactured since mid-2010 have external storage as part of on-board flash, not as removable media.

Thursday, June 3, 2021
answered 7 Months ago

This is not the answer but a workaround.

File file = new File("some_temp_path"); # you can also use app's internal cache to store the file
FileOutputStream fos = new FileOutputStream(file);

InputStream is = context.getContentResolver().openInputStream(uri);
byte[] buffer = new byte[1024];
int len = 0;
try {
    len =;
    while (len != -1) {
        fos.write(buffer, 0, len);
        len =;

} catch (IOException e) {

pass this file's absolute path to your activity.

Wednesday, November 10, 2021
Nicola Pesavento
answered 4 Weeks ago

Sounds like file permissions on your XML files, doesn't it?

If you're executing from a web context, please bear in mind that the web user (for example, "nobody" under Linux/Apache, or "IUSR_MACHINE" under Windows/IIS) has MINIMAL privileges to access your filesystem.

And this is a Good Thing: especially if your application is exposed to the Internet :)

PS: Also, the directory path you cited doesn't look right:


Are you sure it's not supposed to be "C:Program Dataconfig.xml"????

PPS: While we're talking about "file permissions"; Windows Vista, Windows 7 and Server 2008 all have stricter rules against accessing anything in a drive's root (EX: "c:") or system directories (EX: "c:windows" or "c:Program files").

Sunday, November 21, 2021
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :