2014-09-02 58 views
0

中執行我需要在註冊期間將註冊憑證寫入文件,並且在登錄期間將檢查該文件的憑證。但在註冊按鈕上單擊什麼都沒有執行。文件寫入沒有在android

清單文件包含此權限:

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

的registration.java部分:

reg.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String user=username.getText().toString(); 
      String pwd=password.getText().toString(); 
      String cpwd=confirm.getText().toString(); 

      if(user.equals("")||pwd.equals("")||cpwd.equals("")){ 
       Toast.makeText(getApplicationContext(), "Fields cannot be Blank", Toast.LENGTH_SHORT).show(); 
      } 
      else if(pwd.equals(cpwd)){ 
       String data=user+","+pwd+":";     

       try { 
        FileOutputStream fout = new FileOutputStream(Environment.getExternalStorageDirectory()+"/myapp/userregister.txt/", true); 
        fout.write(data.getBytes()); 
        Toast.makeText(getApplicationContext(), "Succesfully Register", Toast.LENGTH_SHORT).show(); 
        Intent a=new Intent(getApplicationContext(),LoginActivity.class); 
        startActivity(a); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
       else if(!pwd.equals(cpwd)){ 
        Toast.makeText(getApplicationContext(), "Password not match.. Try again!", Toast.LENGTH_SHORT).show(); 
       } 


     } 
    }); 

在LoginActivity.java

if(username.equals("") || password.equals("")){ 

     Toast.makeText(getApplicationContext(), "Fields Cannot be Blank", Toast.LENGTH_SHORT).show(); 
    }else{ 
     String reply=FileManipulator.authenticateUser(username, password); 
       if(reply.equals("user")){ 
        Intent gotoNextActivity=new Intent(getApplicationContext(),HomeActivity.class); 
         startActivity(gotoNextActivity); 

       }else{ 
     Toast.makeText(getApplicationContext(), "Invalid User.... Try again",Toast.LENGTH_SHORT).show(); 
      } 
    } 

FileManipulator類的authenticateUser功能:

public static String authenticateUser(String user, String pass){ 
    String data=""; 
    String data1 = ""; 
    try{ 
     FileInputStream fin=new FileInputStream(Path.userregister); 
     byte[] b=new byte[fin.available()]; 
     fin.read(b); 
     data=new String(b); 
     String st=user+","+pass+":"; 
     if(data.contains(st)){ 
      data1="user"; 
     } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

    return data1; 

} 

日誌貓顯示

09-02 05:48:06.292: D/dalvikvm(1964): GC_CONCURRENT freed 240K, 12% free 2838K/3200K, paused 5ms+32ms, total 177ms 
09-02 05:48:06.802: I/Choreographer(1964): Skipped 36 frames! The application may be doing too much work on its main thread. 
09-02 05:48:10.362: I/Choreographer(1964): Skipped 32 frames! The application may be doing too much work on its main thread. 
09-02 05:48:20.872: W/System.err(1964): java.io.FileNotFoundException: /mnt/sdcard/myapp/userregister.txt: open failed: EACCES (Permission denied) 
09-02 05:48:20.872: W/System.err(1964):  at libcore.io.IoBridge.open(IoBridge.java:416) 
09-02 05:48:20.882: W/System.err(1964):  at java.io.FileOutputStream.<init>(FileOutputStream.java:88) 
09-02 05:48:20.882: W/System.err(1964):  at java.io.FileOutputStream.<init>(FileOutputStream.java:128) 
09-02 05:48:20.882: W/System.err(1964): at com.example.text.RegActivity$1.onClick(RegActivity.java:49) 
09-02 05:48:20.892: W/System.err(1964): at android.view.View.performClick(View.java:4204) 
09-02 05:48:20.892: W/System.err(1964): at android.view.View$PerformClick.run(View.java:17355) 
09-02 05:48:20.902: W/System.err(1964): at android.os.Handler.handleCallback(Handler.java:725) 
09-02 05:48:20.912: W/System.err(1964): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-02 05:48:20.912: W/System.err(1964): at android.os.Looper.loop(Looper.java:137) 
09-02 05:48:20.912: W/System.err(1964): at android.app.ActivityThread.main(ActivityThread.java:5041) 
09-02 05:48:20.922: W/System.err(1964): at java.lang.reflect.Method.invokeNative(Native Method) 
09-02 05:48:20.922: W/System.err(1964): at java.lang.reflect.Method.invoke(Method.java:511) 
09-02 05:48:20.922: W/System.err(1964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
09-02 05:48:20.922: W/System.err(1964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
09-02 05:48:20.922: W/System.err(1964): at dalvik.system.NativeStart.main(Native Method) 
09-02 05:48:20.932: W/System.err(1964): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) 
09-02 05:48:20.942: W/System.err(1964): at libcore.io.Posix.open(Native Method) 
09-02 05:48:20.942: W/System.err(1964): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
09-02 05:48:20.952: W/System.err(1964): at libcore.io.IoBridge.open(IoBridge.java:400) 
09-02 05:48:20.952: W/System.err(1964): ... 14 more 

我是一個新手,Android和java..please幫我找到錯誤。

+1

你有權限在清單中嗎? – 2014-09-02 05:44:06

+1

我懷疑日誌顯示了實際的異常,而不僅僅是它發生了什麼......所以,有什麼例外? – 2014-09-02 05:45:06

+0

檢查你的清單你有沒有使用給出完整的日誌,以便我們可以檢查錯誤 – 2014-09-02 05:45:22

回答

2

1)在AndroidManifest.xml文件中添加android.permission.WRITE_EXTERNAL_STORAGE

2)通過寫入/讀取之前,請務必檢查外部存儲的可用性:

String state = Environment.getExternalStorageState(); 
if (Environment.MEDIA_MOUNTED.equals(state)) { 
     // Can read and write the media 

    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     // Can only read the media 

    } else { 
     // Can't read or write 
    } 

3)創建一個目錄,然後讓你的文件了。

File dir = new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/myapp"); 
     dir.mkdirs(); 
     File file = new File(dir, "userregister.txt"); 

注意: 如果您使用的是Android 4.4(KitKat)系統,應用程序不允許寫入二次外部存儲設備,除了在其特定的包目錄

原因

WRITE_EXTERNAL_STORAGE權限只能授予對設備上主外部存儲的寫入訪問權限。除了合成權限所允許的軟件包專用目錄外,不得允許應用程序寫入輔助外部存儲設備。以這種方式限制寫入可確保系統在卸載應用程序時清理文件。