Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file in android emulator

Tags:

android

I have some problems with the creation of a file. For instance, I want to create a file on the sdcard and first i want to check whether file exists or not. If file not exist i will create one and write some text in otherwise if it exists i will append it some text.

like image 844
Curely Nara Avatar asked Sep 03 '10 09:09

Curely Nara


People also ask

How do I add files to AVD?

To copy a file from the connected emulator/device onto the computer, use the following command: adb.exe pull /data/app/<filename> c:\ NOTE When using the adb.exe utility to pull or push files from or into the emulator, ensure that only one AVD is running.

How do I access files on Android Emulator?

To open the Device Explorer, select View > Tool Windows > Device File Explorer or click the Device File Explorer button in the tool window bar. Select a device from the list. Interact with the device content in the file explorer window: Right-click a file or directory to create a new file or directory.


1 Answers

     String state = Environment.getExternalStorageState();

                if (Environment.MEDIA_MOUNTED.equals(state)) 
                {
                     //SDcard is available
                       File f=new File("/sdcard/test.txt");
                       if (!f.exists()) 
                       {
                        //File does not exists
                        f.createNewFile();
                       }

                      //take your inputstream and write it to your file

                      OutputStream out=new FileOutputStream(f);
                      byte buf[]=new byte[1024];
                      int len;
                      while((len=inputStream.read(buf))>0)
                      out.write(buf,0,len);
                      out.close();
                      inputStream.close();
                      System.out.println("\nFile is created...................................");


                }

Dont forget to add the following permission to manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
like image 109
DeRagan Avatar answered Sep 28 '22 04:09

DeRagan