【问题标题】:Unable to open file "/mnt/sdcard/test.txt"无法打开文件“/mnt/sdcard/test.txt”
【发布时间】:2013-08-28 19:49:53
【问题描述】:

由于无法找到文件,我不断收到错误消息。 我用android模拟器模拟代码

java.io.FileNotFoundException :/mnt/sdcard/test.txt: 打开失败: EACCES(权限被拒绝)打开失败

请帮忙..谢谢

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; 
                } 
    } 
} 

【问题讨论】:

  • 我猜你正在使用 Android ,所以我添加了 Android 标签。
  • 检查你是否在模拟器上声明了一些sdcard存储空间。还要看看你是否在andorid manifest中提到了使用外部存储的权限。
  • 尝试 String path=Environment.getExternalStorageDirectory().getAbsolutePath();文件 f = new File(path+"/test.txt");
  • 是的,我已经在模拟器上添加了 sdcard 存储空间!外部存储权限...尚未声明

标签: java android


【解决方案1】:

在你的 manifest.xml 中添加这一行:

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

然后你就可以在外部存储中写入了。

【讨论】:

    【解决方案2】:

    这样使用:

    String file = Environment.getExternalStorageDirectory()+"/test.txt";
    
    File f = new File(file);
    if(f.exists()){
       f.createNewFile(); 
    }
    // your code...
    

    还在 android manifest 文件中添加以下权限

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

    【讨论】:

    • 我改成 String file = Environment.getExternalStorageDirectory()+"/test.txt";现在我打开失败:ENOENT(没有这样的文件或目录)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2016-08-15
    相关资源
    最近更新 更多