2011-04-09 84 views
0

我在我的SD卡文件夾中有多個文本文件,並且我構建了一個允許用戶選擇所需文件的活動。如何從android應用程序獲取文件的完整路徑

但stil,我必須使用傳遞文件路徑到文件構造函數(請參見line2)。 textfile.java:

File sdcard = Environment.getExternalStorageDirectory(); 

     //Get the text file 

     File file = new File(sdcard,"my/ans.txt");//line2 

     //ob.pathh 
     //Read text from file 

     StringBuilder text = new StringBuilder(); 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 

     } 

我想的第二個參數即文件路徑將被存儲爲字符串。
有沒有什麼辦法可以得到所選文件的完整路徑。
我已經使用的代碼如下:

AndroindApplication的.java:

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 
import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

public class AndroindApplication extends ListActivity { 

private List<String> item = null; 
public List<String> path = null; 
private String root="/"; 
private TextView myPath; 
public String pathh; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sdcard); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir("/sdcard/"); 
    } 

    private void getDir(String dirPath) 
    { 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 

    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 


    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 
     path.add(file.getPath()); 
     if(file.isDirectory()) 
     item.add(file.getName() + "/"); 
     else 
     item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

@Override 
protected void onListItemClick(ListView l, View view, int position, long id) { 

    File file = new File(path.get(position)); 
    pathh=path.get(position); 
    Log.e("path="+path,"dxgx"); 
    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 

    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
     Intent myIntent = new Intent(view.getContext(), textfile.class);//goes to textfile.java 
     startActivityForResult(myIntent, 0); 


    } 
} 
} 

我需要的絕對路徑,這樣我可以把它傳遞給

File file = new File(sdcard,"my/ans.txt");//line2 

和Don」當必須打開一個新文件時,不得不在代碼中指定文件路徑

是否有我可以從AndroidApplication.java獲取它?

回答

4

如果我明白你想要什麼,你可以使用getAbsolutePath()方法來獲取文件的完整路徑。

編輯:

存儲路徑到文件夾,即

String myPath = sdcardEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/my/"; 

,然後訪問這些文件爲:

File currentFile = new File(myPath + path.get(position)); 
+0

我file.getAbsolutePath(試過),並把它裏面Log.d(「MyApp」,file.getAbsolutePath());在輸出中,它給了sd [/ – ProgramME 2011-04-09 16:53:36

+0

它沒有給出完整路徑.i在文件file = new File(path.get (位置)); – ProgramME 2011-04-09 16:54:26

+0

是'my/ans.txt'資源文件還是文件,它的完整路徑是'/ mnt/sdcard/my/ans.txt'或類似的東西? – MByD 2011-04-09 17:03:57

相關問題