android中sdcard,从android中的sdcard读取数据

这篇博客讲述了如何在Android应用中列出SDCard上的文件,并在点击文件时根据文件类型显示内容。作者首先展示了一个用于列出目录的ListActivity代码,然后解释了如何通过Intent来查看不同类型的媒体文件,如图片、视频和音频。当点击非媒体文件时,代码尝试在TextView中显示文本内容。

我只是想在模拟器上显示来自sdcard的文件内容(如图像文件/视频文件/音乐文件那样).

以下是我的代码.

public class listfiles extends ListActivity {

private ArrayList item = null;

private ArrayList path = null;

private String root="/";

private TextView myPath;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.sub);

myPath = (TextView)findViewById(R.id.path);

getDir(root);

}

private void getDir(String dirPath)

{

myPath.setText("Location: " + dirPath);

item = new ArrayList();

path = new ArrayList();

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 fileList =

new ArrayAdapter(this,R.layout.row,item);

setListAdapter(fileList);

}

@Override

protected void onListItemClick(ListView l,View v,int position,long id) {

File file = new File(path.get(position));

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() {

@Override

public void onClick(DialogInterface dialog,int which) {

// TODO Auto-generated method stub

}

}).show();

}

}

else

{

new AlertDialog.Builder(this)

.setIcon(R.drawable.icon)

.setTitle("[" + file.getName() + "]")

.setPositiveButton("OK",int which) {

// TODO Auto-generated method stub

}

}).show();

}

}

}

在我的输出中,我得到了文件路径&文件名.但是当我点击文件时,它不会显示内容.我该怎么办?谢谢

最后我明白了.我修改后的代码如下所示..

public class SDCardActivity extends ListActivity {

private List item = null;

private List path = null;

private String root="/sdcard";

private TextView myPath;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Intent intent=getIntent();

setContentView(R.layout.sub);

myPath = (TextView)findViewById(R.id.path);

getDir(root);

}

private void getDir(String dirPath)

{

myPath.setText("Location: " + dirPath);

item = new ArrayList();

path = new ArrayList();

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 fileList =

new ArrayAdapter(this,new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,int which){

// TODO Auto-generated method stub

dialog.dismiss();

}

}).show();

}

}

else

{

Intent intent = new Intent();

intent.setAction(Intent.ACTION_VIEW);

Uri uri = Uri.parse("file://" + file.getPath());

String fname=file.getName();

if(fname.endsWith(".jpeg")||fname.endsWith("png")||fname.endsWith(".gif"))

{

intent.setDataAndType(uri,"image/*");

startActivity(intent);

}

else if(fname.endsWith(".mp4")||fname.endsWith(".3gp"))

{

intent.setDataAndType(uri,"video/*");

startActivity(intent);

}

else if(fname.endsWith(".mp3"))

{

intent.setDataAndType(uri,"audio/*");

startActivity(intent);

}

else

try {

EditText tv = (EditText)findViewById(R.id.tn);

StringBuilder text = new StringBuilder();

BufferedReader br = new BufferedReader(new FileReader(file));

String line;

while ((line = br.readLine()) != null) {

text.append(line);

text.append('\n');

//Set the text

tv.setText(text);

}

}//try

catch (IOException e) {

//You'll need to add proper error handling here

}//catch

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值