gpt4 book ai didi

android - 从android中的sdcard读取数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:24 26 4
gpt4 key购买 nike

我只想在模拟器上显示 sdcard 中的文件内容(如图像文件/视频文件/音乐文件等)。

下面是我的代码。

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();
}
}
}

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

终于我明白了。我更正的代码如下所示..

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

}
}
}

最佳答案

下面的代码向您展示了如何从SD卡中读取文件内容。只需在SD卡中插入一个文本文件并在您的程序中实现以下代码即可。

    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();
}

关于android - 从android中的sdcard读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4633260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com