从SD卡中的Android读取数据 [英] read data from sdcard in android
问题描述
我只是想从SD卡显示在模拟器文件的内容(如图像文件/视频文件/这样的音乐文件)。
下面是我的code。
公共类listfiles扩展ListActivity {
私人的ArrayList<字符串>项目= NULL;
私人的ArrayList<字符串> PATH = NULL;
私人字符串的root =/;
私人TextView的mypath中;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.sub);
mypath中=(TextView中)findViewById(R.id.path);
GETDIR(根);
}
私人无效GETDIR(字符串dirPath)
{
myPath.setText(位置:+ dirPath);
项目=新的ArrayList<字符串>();
路径=新的ArrayList<字符串>();
文件F =新的文件(dirPath);
文件[]文件= f.listFiles();
如果(!dirPath.equals(根))
{
item.add(根);
path.add(根);
item.add(../);
path.add(f.getParent());
}
的for(int i = 0; I< files.length;我++)
{
文件file =文件[I]
path.add(file.getPath());
如果(file.isDirectory())
item.add(file.getName()+/);
其他
item.add(file.getName());
}
ArrayAdapter<字符串>的fileList =
新的ArrayAdapter<字符串>(这一点,R.layout.row,项目);
setListAdapter(fileList)中的;
}
@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
档案文件=新的文件(path.get(位置));
如果(file.isDirectory())
{
如果(file.canRead())
GETDIR(path.get(位置));
其他
{
新AlertDialog.Builder(本)
.setIcon(R.drawable.icon)
.setTitle([+ file.getName()+]文件夹无法读取!)
.setPositiveButton(OK,
新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
}
})。显示();
}
}
其他
{
新AlertDialog.Builder(本)
.setIcon(R.drawable.icon)
.setTitle([+ file.getName()+])
.setPositiveButton(OK,
新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
}
})。显示();
}
}
}
在我的输出我得到的文件路径和放大器;文件名。但是当我点击该文件,它不会显示的内容。我应该怎么做呢?谢谢
最后我得到it.My纠正code如下图所示。
公共类SDCardActivity扩展ListActivity {
私人列表<字符串>项目= NULL;
私人列表<字符串> PATH = NULL;
私人字符串的root =/ SD卡;
私人TextView的mypath中;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//意向意图= getIntent();
的setContentView(R.layout.sub);
mypath中=(TextView中)findViewById(R.id.path);
GETDIR(根);
}
私人无效GETDIR(字符串dirPath)
{
myPath.setText(位置:+ dirPath);
项目=新的ArrayList<字符串>();
路径=新的ArrayList<字符串>();
文件F =新的文件(dirPath);
文件[]文件= f.listFiles();
如果(!dirPath.equals(根))
{
item.add(根);
path.add(根);
item.add(../);
path.add(f.getParent());
}
的for(int i = 0; I< files.length;我++)
{
文件file =文件[I]
path.add(file.getPath());
如果(file.isDirectory())
item.add(file.getName()+/);
其他
item.add(file.getName());
}
ArrayAdapter<字符串>的fileList =
新的ArrayAdapter<字符串>(这一点,R.layout.row,项目);
setListAdapter(fileList)中的;
}
@覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
档案文件=新的文件(path.get(位置));
如果(file.isDirectory())
{
如果(file.canRead())
GETDIR(path.get(位置));
其他
{
新AlertDialog.Builder(本)
.setIcon(R.drawable.icon)
.setTitle([+ file.getName()+]文件夹无法读取!)
.setPositiveButton(OK,
新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
dialog.dismiss();
}
})。显示();
}
}
其他
{
意向意图=新的意图();
intent.setAction(Intent.ACTION_VIEW);
开放的我们的uri = Uri.parse(文件://+ file.getPath());
字符串FNAME = file.getName();
如果(fname.endsWith(JPEG)|| fname.endsWith(PNG)|| fname.endsWith(GIF))
{
intent.setDataAndType(URI,图像/ *);
startActivity(意向);
}
否则如果(fname.endsWith(。MP4)|| fname.endsWith(。3GP))
{
intent.setDataAndType(URI,视频/ *);
startActivity(意向);
}
否则,如果(fname.endsWith(MP3))
{
intent.setDataAndType(URI,音频/ *);
startActivity(意向);
}
其他
尝试 {
EditText上电视=(EditText上)findViewById(R.id.tn);
StringBuilder的文本=新的StringBuilder();
的BufferedReader BR =新的BufferedReader(新的FileReader(文件));
串线;
而((行= br.readLine())!= NULL){
text.append(线);
text.append('\ N');
//设置文本
tv.setText(文本);
}
}//尝试
赶上(IOException异常E){
//你需要在这里添加正确的错误处理
}//抓住
}
}
}
也许我已经错过了在你的code,但我不能发现任何的意图在里面。你必须调用意图
与 ACTION_VIEW
标志的任何文件,你要显示出来。
有关实例。
意向意图=新的意图();
intent.setAction(Intent.ACTION_VIEW);
乌里imgUri = Uri.parse(文件://+ file.getPath());
intent.setDataAndType(imgUri,图像/ *);
startActivity(意向);
您简单的创建的实例意图
,请在我们的例子是动作 ACTION_VIEW
。然后你通过连接到文件中的文件对象的路径中创建一个
。所有你现在要做的是通过指定的URI和一个字符串类型设定上的意图的数据类型。在我的例子每个图像类型。你可以不过只指定一个特定的图像类型。一旦你的意图成立,并准备你通过启动一个活动,意图作为参数火它关闭。乌里
对象://
Android的是要采取寻找一个合适的应用程序,以显示在意向中的数据服务。
I just wanna display the contents of files from sdcard on emulator (as image files/video files/music files like that).
Below is my code.
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();
}
}
}
In my output I got file path & file name. But when i click the file, it won't show the contents. What should I do for that? Thanks
Finally i got it.My corrected code is shown below..
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
}
}
}
Maybe I've missed it in your code but I could not spot any Intent in it. You have to call an Intent
with the ACTION_VIEW
flag for whatever file you want to be shown.
For instance.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + file.getPath());
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
You simple create an instance of Intent
, set the action which is ACTION_VIEW
in our case. Then you create an Uri
object by concatenating the path of your file object to file://
. All you have to do now is setting the data and type on the intent by specifying the uri and a type string. In my example every image type. You could however just specify a certain image type. Once your intent is set up and ready you fire it off by starting an Activity with the intent as a parameter.
Android is going to take care of finding an appropriate application to display the data in the intent.
这篇关于从SD卡中的Android读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!