如何获得文件从Android应用程序的完整路径 [英] How to get the complete path of file from android application
问题描述
我有多个文本文件在我的SD卡文件夹和我有建立一个活动,允许用户选择所需的文件。
i have multiple text files in my sdcard folder and i have build an activity that allows user to select the required file.
但还是老样子,我必须使用通过文件路径到文件的构造函数(见2号线)。
textfile.java:
but stil,i have to use pass the filepath to file constructor(see 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));
}
我想第二个参数,即文件路径存储为字符串。结果
有没有什么办法可以获取文件的选择的完整路径。结果
在code我用如下:
i want the second parameter ie filepath to be stored as string.
is there any way i can get the complete path of file selected .
The code i had used is as follows:
AndroindApplication的.java:
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);
}
}
}
我需要得到的绝对路径,这样我可以把它传递给
I need to get the absolute path so that i can pass it to
File file = new File(sdcard,"my/ans.txt");//line2
和没有指定在code中的文件路径whenver一个新的文件已被打开。
and don't have to specify the filepath in the code whenver a new file has to be opened
有反正我可以从AndroidApplication.java怎么做呢?
Is there anyway i can get it from AndroidApplication.java ?
推荐答案
如果我知道你想什么,你可以使用 getAbsolutePath()
方法获取的完整路径一个文件。
If I understand what you want, you can use getAbsolutePath()
method to get the full path of a file.
编辑:
存储的路径到你的文件夹,即
Store the path to your folder, i.e.
String myPath = sdcardEnvironment.getExternalStorageDirectory().getAbsolutePath() + "/my/";
,然后访问这些文件为:
and then access the files as:
File currentFile = new File(myPath + path.get(position));
这篇关于如何获得文件从Android应用程序的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!