0

I keep getting error by not being able to find the files. i used the android emulator to simulate the code

java.io.FileNotFoundException :/mnt/sdcard/test.txt: open failed: EACCES (Permission denied) open failed

please help.. thanks

public class TestUpload extends Activity { 

  /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_test_upload); 

        final TextView tmp = (TextView) findViewById(R.id.textView1); 
        tmp.setText("Hi! Click the button!"); 

        Button b = (Button) findViewById(R.id.button1); 
        b.setOnClickListener(new OnClickListener() { 
                public void onClick(View v) { 
                        File f = new File("/mnt/sdcard/test.txt"); 
                        try { 
                                        f.createNewFile(); 
                                        Date d = new Date(); 
                                        PrintWriter writer = new PrintWriter(f);
                                        writer.println(d.toString()); 
                                        writer.close(); 

                                        HttpClient client = new DefaultHttpClient();
                                         httpPostFileUpload(client, "/mnt/sdcard/test.txt", "http://localhost/upload.php", "uploadedfile"); 
                        } catch (Exception e) { 
                                        // TODO Auto-generated catch block 
                                        e.printStackTrace(); 
                                } 
                } 
        }); 
    } 

    public void httpPostFileUpload(HttpClient client,String filePath,String uploadUri,String inputNameAttr) throws ClientProtocolException,IOException { 

            HttpUriRequest         request         = new HttpPost(uploadUri); 
            MultipartEntity        form                = new MultipartEntity(); 
            client.getParams().setBooleanParameter("http.protocol.expect-continue", false); 
            form.addPart(inputNameAttr, new FileBody(new File(filePath))); 

                ((HttpEntityEnclosingRequestBase) request).setEntity(form); 

                try { 
                        client.execute(request); 
                } catch (ClientProtocolException e) { 
                        throw e; 
                } catch (IOException ee) { 
                        throw ee; 
                } 
    } 
} 
Raptor
  • 53,206
  • 45
  • 230
  • 366
user2408603
  • 11
  • 1
  • 5
  • I guess you are working on Android , so I add Android tag. – Raptor May 22 '13 at 08:33
  • Check whether you have declared some sdcard storage space over the emulator. Also see if you have mentioned the permission for using external storage in andorid manifest. – Yogesh Patil May 22 '13 at 08:34
  • try String path=Environment.getExternalStorageDirectory().getAbsolutePath(); File f = new File(path+"/test.txt"); – Athul Harikumar May 22 '13 at 08:37
  • yup i have added sdcard storage space already over the emulator! permission for external storage... have not declared – user2408603 May 22 '13 at 08:37
  • possible duplicate [Permission denied when writting into sdCard](http://stackoverflow.com/q/14475951/1699210) – bummi Aug 28 '13 at 12:23

2 Answers2

0

Add this line in yout manifest.xml :

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

Then you will be able to write in external storage.

Fabien Sa
  • 9,135
  • 4
  • 37
  • 44
0

use this way:

String file = Environment.getExternalStorageDirectory()+"/test.txt";

File f = new File(file);
if(f.exists()){
   f.createNewFile(); 
}
// your code...

also add below permission in android manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • i changed to String file = Environment.getExternalStorageDirectory()+"/test.txt"; now i get open failed: ENOENT(No such file or directory) – user2408603 May 22 '13 at 08:47