在android中从SD卡读取数据
我只想在emulator上显示SD卡中文件的内容(像这样的图像文件/视频文件/音乐文件) 下面是我的代码在android中从SD卡读取数据,android,file,Android,File,我只想在emulator上显示SD卡中文件的内容(像这样的图像文件/视频文件/音乐文件) 下面是我的代码 public class listfiles extends ListActivity { private ArrayList<String> item = null; private ArrayList<String> path = null; private String root="/"; private TextView myPath; /*
public class listfiles extends ListActivity {
private ArrayList<String> item = null;
private ArrayList<String> 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<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 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",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
公共类listfiles扩展ListActivity{
私有ArrayList项=null;
私有ArrayList路径=null;
私有字符串root=“/”;
私有文本视图myPath;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sub);
myPath=(TextView)findViewById(R.id.path);
getDir(根);
}
私有void getDir(字符串dirPath)
{
setText(“位置:“+dirPath”);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(dirPath);
File[]files=f.listFiles();
如果(!dirPath.equals(root))
{
项。添加(根);
添加(根目录);
项目.添加(“/”);
add(f.getParent());
}
对于(int i=0;i
在我的输出中,我得到了文件路径和文件名。但是当我单击文件时,它不会显示内容。我该怎么办?谢谢
我终于明白了。我更正的代码如下所示
public class SDCardActivity extends ListActivity {
private List<String> item = null;
private List<String> 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<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 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() {
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
}
}
}
公共类SDCardActivity扩展了ListActivity{
私有列表项=null;
私有列表路径=null;
私有字符串root=“/sdcard”;
私有文本视图myPath;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Intent=getIntent();
setContentView(R.layout.sub);
myPath=(TextView)findViewById(R.id.path);
getDir(根);
}
私有void getDir(字符串dirPath)
{
setText(“位置:“+dirPath”);
item=newarraylist();
路径=新的ArrayList();
文件f=新文件(dirPath);
File[]files=f.listFiles();
如果(!dirPath.equals(root))
{
项。添加(根);
添加(根目录);
项目.添加(“/”);
add(f.getParent());
}
对于(int i=0;i
也许我在你的代码中漏掉了它,但我在代码中找不到。对于要显示的任何文件,都必须使用ACTION\u VIEW
标记调用Intent
比如说
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + file.getPath());
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
您只需创建一个Intent
的实例,在我们的例子中,将动作设置为action\u VIEW
。然后通过连接文件的路径来创建一个Uri
对象
try{
File f = new File(Environment.getExternalStorageDirectory()+"/f1.txt");
fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line and pass it on the debugger
while((readString = buf.readLine())!= null){
textdata.setText(readString);
Log.d("line: ", readString);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}