package com.paad.trial;
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;
static public List<String> path = null;
private String root="/";
private TextView myPath;
static public int pathh;
private Object sdcardEnvironment;
/** 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=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(), Readfile.class);
startActivityForResult(myIntent, 0);
Bundle b=new Bundle();
// Bundle
//myIntent.p
/* new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();*/
}
}
}
package com.paad.trial;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Readfile extends Activity {
/** Called when the activity is first created. */
AndroindApplication ob=new AndroindApplication();
Intent i;
String[] arr=null;
private Button pButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
//File sdcard = Environment.getExternalStorageDirectory();
String myPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfolder/";
//Get the text file
File file = new File(myPath + AndroindApplication.path.get(AndroindApplication.pathh));
//ob.pathh
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line=null;
//int i=0;
List<String> lines = new ArrayList<String>();
while ((line = br.readLine()) != null) {
lines.add(line);
// arr[i]=line;
// i++;
text.append(line);
text.append('\n');
}
arr = lines.toArray(new String[lines.size()]);
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
pButton = (Button) findViewById(R.id.Button01);
pButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bundle b=new Bundle();
b.putStringArray("strings", arr);
i=new Intent(v.getContext(),texttospeech.class);
i.putExtras(b);
startActivityForResult(i, 0);
}
});
}
}
my sdcard contains two text files. I want when the user selects one of them within the application then it should open the selected file. In the above code I am trying to pass the variable path,postion from AndroindApplication.java to Readfile.java .But in Readfile it is unable to get the path.
The text files are stored in myfolder under sdcard. The two varaibles are used by File Oject in readfile.java to read the file.
HELP