ProgramME
ProgramME

Reputation: 653

How to get the complete path of file from android application

i have multiple text files in my sdcard folder and i have build an activity that allows user to select the required file.

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

         } 

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:

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  

and don't have to specify the filepath in the code whenver a new file has to be opened

Is there anyway i can get it from AndroidApplication.java ?

Upvotes: 0

Views: 30060

Answers (1)

MByD
MByD

Reputation: 137272

If I understand what you want, you can use getAbsolutePath() method to get the full path of a file.

Edit:

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

Upvotes: 4

Related Questions