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()
现在正在学习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()
解决方案 »
- android 在JNI回调APK的方法时发生了错误? JNI WARNING: threadid=10 using env from threadid=1
- 关于摄像头对焦问题
- Android的系统开发流程???
- 能否通过代码来管理程序列表?比如删除一个指定的程序?
- gridview里item的变换
- 怎样从一个界面跳转到百度一下界面?并且将值显示到百度一下的框里?
- 可不可以用反射机制来获取android底层传递的数据,求大神!!!
- android sdk manager刷出来的可下载包对不上,咋整吖!?
- 上传大文件的问题
- webView.loadDataWithBaseURL()加载的html不显示图片
- 服务器推送XMPP协议
- OTA只是重启,而进不了recovery
没太明白,请详细说一说。
或直接改read_file()。谢谢!
没太明白,请详细说一说。
或直接改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 ;
}
}
// TODO Auto-generated method stub
Resources mResources=null;
mResources = getResources();
return mResources;
}
要将下句
InputStream in = getResources().getAssets().open(fileName);
改为:
InputStream in = mContext.getResources().getAssets().open(fileName);编译通过了,可运行显示没结果呀!
谢谢大家!