繁体   English   中英

如何从Android应用程序获取文件的完整路径

[英]How to get the complete path of file from android application

我的sdcard文件夹中有多个文本文件,并且我建立了一个活动,允许用户选择所需的文件。

但是stil,我必须使用将文件路径传递给文件构造函数(请参阅第2行)。 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  

并且每次必须打开新文件时都不必在代码中指定文件路径

无论如何,我可以从AndroidApplication.java获得它吗?

如果我了解您想要的内容,则可以使用getAbsolutePath()方法获取文件的完整路径。

编辑:

将路径存储到您的文件夹,即

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

然后以下列方式访问文件:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM