2

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);
    }
}
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
Makame Sharif
  • 65
  • 1
  • 1
  • 8

2 Answers2

3

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

Community
  • 1
  • 1
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • Thank your for your help but steel i have problem when i submit i get this error "Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)". this problem i do not know why happen it because i have allowed permission manifest.xml. below is my manifest`s codes – Makame Sharif Jul 02 '15 at 11:57
  • Thank you for your help but steel iget the following error. "Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)" i dot know why occur it because i have allowed permission in manifest.xml – Makame Sharif Jul 02 '15 at 12:02
  • what worked for me, simply going to application settings and revoking the storage permission and then giving it again, worked for me – beginner Sep 02 '20 at 09:32
1

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.

Rubanraj Ravichandran
  • 1,213
  • 2
  • 17
  • 26