2011-04-09 8 views
0

내 sdcard 폴더에 여러 텍스트 파일이 있고 사용자가 필요한 파일을 선택할 수있는 활동을 빌드했습니다.안드로이드 응용 프로그램에서 파일의 전체 경로를 얻는 방법

하지만 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 : 내가

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

'과'돈 '에 전달할 수 있도록 내가 절대 경로를 얻을 필요가

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


    } 
} 
} 

을 다음과 같이 내가 사용했다
코드는 새로운 파일을 열어야 할 때마다 코드에서 파일 경로를 지정해야합니다.

어쨌든 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

+0

]이 전체 경로를 제공하지 않음 .i 파일 file = new File (path.get (위치)); – ProgramME

+0

은'my/ans.txt' 리소스 파일이거나 전체 경로가'/ mnt/sdcard/my/ans.txt' 또는 비슷한 파일입니까? – MByD

관련 문제