Makame Sharif
Makame Sharif

Reputation: 65

I am using Android studio I want to write local text file via emulator

I want to write text file in local pc via emulator and i have try a lot of way but not yet successes so i need help for any one who will recognized what my problem. below is my codes. This code work without an error but working less.

public void WriteText() {
    EditText txt=(EditText)findViewById(R.id.txtwrite);


    try {
        FileOutputStream fos = openFileOutput("D:/File.txt", Context.MODE_PRIVATE);
        fos.write(txt.getText().toString().getBytes());
        fos.close();
        Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
    } catch (Exception e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
    }
}

Upvotes: 2

Views: 11095

Answers (2)

Rubanraj Ravichandran
Rubanraj Ravichandran

Reputation: 1293

You cannot save files from emulator to PC. Because, Emulator is like a seperate device so it is not possible to create files on PC. Instead of that, you can do like this work around.

Create and save text file in your emulator storage

public void WriteText() {
EditText txt=(EditText)findViewById(R.id.txtwrite);


try {
    BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
    fos.write(txt.getText().toString().trim());
    fos.close();
    Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
    Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
    }
}

Once you done with your file creation, you can easily pull the created file from emulator to your pc using adb pull command.

ADB command

adb pull /sdcard/File.txt D:/

Dont forget to add following uses-permission in your manifest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Note: Give some space for your emulator SDcard when you create your emulator.

Upvotes: 1

Anoop M Maddasseri
Anoop M Maddasseri

Reputation: 10549

Definition of Emulator

The Android SDK includes a mobile device emulator — a virtual mobile device that runs on your computer. The emulator lets you develop and test Android applications without using a physical device.

It's a virtual mobile device.which means an emulator is much like a mobile device.you can only create files inside it not in computer.

Use the following method to create a file in android

Set permission in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  public void createFile(String sFileName, String sBody){
    try
    {
        File root = new File(Environment.getExternalStorageDirectory(), "Notes");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, sFileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append(sBody);
        writer.flush();
        writer.close();
        Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
    }
    catch(IOException e)
    {
         e.printStackTrace();

    }
   } 

For browsing the newly created file How to access files in Emulator SD Card using File Explorer?

If you want to send data from android to a computer system take a look at http://www.pixelstech.net/article/1368328614-Android-socket-programming-example

Upvotes: 3

Related Questions