Android新手,请教问题。
现在正在学习Android,安装和配置了环境,测试了helloworld,一切正常。下面想学习对文件的操作,参考了网上的例子,可以读取assets和sdcard中的文件。
源文件请看下边:
package com.example.helloworld;import java.io.FileInputStream;
import java.io.InputStream;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;public class MainActivity extends Activity {

String str;
String filename;
// private static Context context=null;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.activity_main);       //另一种显示文本的方法
//        str="This is a test.";
//        tv.setText(str);
        
       filename="phone_bj.txt";
       str=readfile(filename);
        
//        filename="/sdcard/phone.txt";
//        str=readfile_sd(filename);
  
        TextView tv=new TextView(this);      
        tv.setText(str);
        setContentView(tv);
    }    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
   
   /*
    public static Context getContext(){
     return context;
    }
 */
    
  //读取assets中文件
    private String readfile(String fileName) {
     String filecontent = null;
    
     try{
     InputStream in = getResources().getAssets().open(fileName);
     int length = in.available();
     byte [] buffer = new byte [length];
     in.read(buffer);
  filecontent = new String(buffer, "GBK");
    
     }catch(Exception e){
     e.printStackTrace();
     }
    
     return filecontent ;
    }
  
  
    //读取sdcard中文件
    private String readfile_sd(String fileName) {
     String filecontent = null;
     try{
     FileInputStream fis = new FileInputStream(fileName);
     int length = fis.available();
     byte [] buffer = new byte [length];
     fis.read(buffer);
     filecontent = new String(buffer, "GBK");
    
     fis.close();
     }catch(Exception e){
     e.printStackTrace();
     }
    
     return filecontent ;
    }    
}我现在的问题是想把有关文件读取操作放到另一个普通class中,类似下面的样子:package com.example.helloworld;import java.io.FileInputStream;
import java.io.InputStream;
import android.content.res.Resources;public class read_file { /*
    private Resources getResources() {
// TODO Auto-generated method stub
     Resources mResources=null;
     mResources = getResources();
return mResources;
}

*/

//读取assets中文件
    private String readfile(String fileName) {
     String filecontent = null;
    
     try{
     InputStream in = getResources().getAssets().open(fileName);
     int length = in.available();
     byte [] buffer = new byte [length];
     in.read(buffer);
  filecontent = new String(buffer, "GBK");
    
     }catch(Exception e){
     e.printStackTrace();
     }
    
     return filecontent ;
    }
  
  
    //读取sdcard中文件
    private String readfile_sd(String fileName) {
     String filecontent = null;
     try{
     FileInputStream fis = new FileInputStream(fileName);
     int length = fis.available();
     byte [] buffer = new byte [length];
     fis.read(buffer);
     filecontent = new String(buffer, "GBK");
    
     fis.close();
     }catch(Exception e){
     e.printStackTrace();
     }
    
     return filecontent ;
    }

}不知如何操作?谢谢大家!
AndroidActivitygetResources()

解决方案 »

  1.   

    getResources()其实activity是父类的一个方法,把activity对象 或者this 或者context作为参数传递给你要的类作为参数就行了,甚至你可以直接用appContext作为上下文调用getResources()
      

  2.   


    没太明白,请详细说一说。
    或直接改read_file()。谢谢!
      

  3.   


    没太明白,请详细说一说。
    或直接改read_file()。谢谢!

    package com.example.helloworld;import java.io.FileInputStream;
    import java.io.InputStream;
    import android.content.res.Resources;
    import android.content.Context;public class read_file {
        
    private Context mContext;    read_file(Context context)
    {
       mContext = context;
    }
    /*
        private Resources getResources() {
    // TODO Auto-generated method stub
         Resources mResources=null;
         mResources = mContext.getResources();
    return mResources;
    }

    */

    //??assets???
        private String readfile(String fileName) {
         String filecontent = null;
        
         try{
         InputStream in = getResources().getAssets().open(fileName);
         int length = in.available();
         byte [] buffer = new byte [length];
         in.read(buffer);
      filecontent = new String(buffer, "GBK");
        
         }catch(Exception e){
         e.printStackTrace();
         }
        
         return filecontent ;
        }
      
      
        //??sdcard???
        private String readfile_sd(String fileName) {
         String filecontent = null;
         try{
         FileInputStream fis = new FileInputStream(fileName);
         int length = fis.available();
         byte [] buffer = new byte [length];
         fis.read(buffer);
         filecontent = new String(buffer, "GBK");
        
         fis.close();
         }catch(Exception e){
         e.printStackTrace();
         }
        
         return filecontent ;
        }

    }
      

  4.   

     Context 可以作为参数传递,就可以使用Context 的public 方法了。 你注释掉的getResources就是其中一种调用。注释掉是什么意思呢,留着会有错误吗 ?有错误贴出来    private Resources getResources() {
            // TODO Auto-generated method stub
            Resources mResources=null;
            mResources = getResources();
            return mResources;
        }
         
       
      

  5.   

    新手,太笨了。
    要将下句
    InputStream in = getResources().getAssets().open(fileName);
    改为:
    InputStream in = mContext.getResources().getAssets().open(fileName);编译通过了,可运行显示没结果呀!
      

  6.   

    按照guoyoulei520的方法解决了,是在Activity中调用读文件方法有误。注释掉的方法,还没太搞懂,过一阵在说。
    谢谢大家!