如何从android应用程序获取文件的完整路径
问题描述:
我在我的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获取它?
答
如果我明白你想要什么,你可以使用getAbsolutePath()
方法来获取文件的完整路径。
编辑:
存储路径到文件夹,即
String myPath = sdcardEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/my/";
,然后访问这些文件为:
File currentFile = new File(myPath + path.get(position));
我file.getAbsolutePath(试过),并把它里面Log.d(“MyApp”,file.getAbsolutePath());在输出中,它给了sd [/ – ProgramME 2011-04-09 16:53:36
它没有给出完整路径.i在文件file = new File(path.get (位置)); – ProgramME 2011-04-09 16:54:26
是'my/ans.txt'资源文件还是文件,它的完整路径是'/ mnt/sdcard/my/ans.txt'或类似的东西? – MByD 2011-04-09 17:03:57