2011-04-10 100 views
0
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);   
    } 
}); 






} 
    } 

我的SD卡包含两个文本文件的多个文本文件。我希望当用户在应用程序中选择其中的一个时,它应该打开选定的文件。 在上面的代码中,我试图将变量路径从AndroindApplication.java传递给Readfile.java。但是在Readfile中,它无法获取路径。无法读取存储在SD卡

文本文件存储在sdcard下的myfolder中。 这两个变量由Readfile.java中的File Oject用来读取文件。

HELP

回答

0

Readfile你没有 - 不应该永远 - 实例AndroindApplication。要在两个活动之间传递字符串,您应该使用Intents。

这对于字符串来说非常简单。见Passing data between activities in Android

+0

谢谢你,但我想通过一个类型的对象列表 – ProgramME 2011-04-11 17:27:34

+0

@ProgramME:我明白了。从你的代码看来,你使用'path'和'position'只是为了得到'path'中的一个字符串,所以我想:为什么不通过它呢? – bigstones 2011-04-11 19:25:27