2015-10-18 76 views
1

在我的應用我創建使用下面的代碼隱藏的文本文件:如何以編程方式在android中打開隱藏的文件?

logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt"); 

if(!logfile.exists()){ 
    try { 
     logfile.createNewFile(); 

     //Toast.makeText(SimpleIME.this,"File created...",Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(SimpleIME.this,"IOException : "+e.getMessage(),Toast.LENGTH_SHORT).show(); 
    } 
} 

這工作得很好。它創建一個隱藏的文件。然後當我按下名爲viewlog的按鈕時,我想再次打開該文本文件。

代碼viewlog是這樣的。

viewlog.setOnClickListener(new OnClickListener() { 

@Override 
    public void onClick(View arg0) { 

     logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt"); 
     Uri uri = Uri.parse("file://" + logfile.getAbsolutePath()); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setData(uri); 
     startActivity(intent); 

    } 

}); 

所以,當我運行這個程序,當我點擊這個按鈕viewlog它強制關閉應用程序。

那麼如何解決這個問題呢?

回答

1

造成的:android.content.ActivityNotFoundException:無活動來處理意圖

logfile = new File(Environment.getExternalStorageDirectory().toString()+ "/.logfile.txt"); 
    Uri uri = Uri.parse("file://" + logfile.getAbsolutePath()); 
    Intent intent = new Intent(Intent.ACTION_VIEW); 
    intent.setDataAndType(uri, "text/plain"); 
    startActivity(intent); 
+0

它works..Thanks救了我.. !!那麼文件夾呢?如果我們創建一個隱藏文件夾並嘗試打開它,應該傳遞給setDataandType參數呢? –

+0

intent.setDataAndType(uri,「*/*」) – kzz

+0

非常感謝幫助 –

相關問題