无法读取多个文本文件存储在SD卡 [英] Unable to read more than one text files stored in sdcard

查看:122
本文介绍了无法读取多个文本文件存储在SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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卡包含两个文本文件。我想,当用户在应用程序内选择其中之一,那么就应该打开所选的文件。
在上面的code我试图从AndroindApplication.java传递变量路径,现在的位置,以Readfile.java。但在READFILE是无法得到的路径。


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.

文本文件存储在MyFolder中下SD卡。
这两个varaibles用于按文件Oject在readfile.java读取该文件。

The text files are stored in myfolder under sdcard. The two varaibles are used by File Oject in readfile.java to read the file.

帮助

推荐答案

READFILE 你没有 - 不应该永远 - 实例 AndroindApplication 。要传递两个活动之间的字符串,你应该使用意图。

In Readfile you don't have - and shouldn't ever - instantiate AndroindApplication. To pass a string between two activities you should use Intents.

这是pretty在字符串的情况下,简单。请参见 Android中传递活动之间的数据

It's pretty simple in the case of a string. See Passing data between activities in Android

这篇关于无法读取多个文本文件存储在SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆