首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

andriod 怎么读取sd卡中指定的文件

2012-02-07 
andriod 如何读取sd卡中指定的文件?以下是读取sd卡中的下载文件,并安装,但是总是提示:文件不存在,重新下载

andriod 如何读取sd卡中指定的文件?
以下是读取sd卡中的下载文件,并安装,但是总是提示:文件不存在,重新下载?"
其实sd卡中已有下载的文件了,就是读取不到,什么原因啊?

void installFile(){

File file = null;
// 获取扩展SD卡设备状态
String sDStateString = android.os.Environment.getExternalStorageState();
// 拥有可读可写权限
if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
// 获取扩展存储设备的文件目录
File SDFile = android.os.Environment.getExternalStorageDirectory();
//String fileNameNew = (fileName + ".zip").substring(0, fileName
//.length() - 5);
//Log.v("fileNameNew==", fileNameNew);
Log.v("fileNameNew==", "file is read and write");
// 打开文件
//file = new File(SDFile.getAbsolutePath() + File.separator
//+ fileName);
file = new File(SDFile.getAbsolutePath() +"/"
+ fileName);
// 判断是否存在,不存在则重新下载
if (!file.exists()) {
Log.v("fileNameNew==,file is exists", "file is not exist");
// myFile.createNewFile();
AlertDialog alertdialog = new AlertDialog.Builder(this)
.setTitle("提示").setMessage("文件不存在,重新下载?")
// .setIcon(R.drawable.quit)
.setPositiveButton("下载",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
setResult(RESULT_OK);// 确定按钮事件
// System.exit(1);
dialog.cancel();
// 调用系统浏览器下载文件
//startActivity(new Intent(
//Intent.ACTION_VIEW, Uri
//.parse(fileUrl)));
downLoadFile(fileUrl);
Log.v("MyActivity,4",
"change into DownLoadFile");
}
}).setNegativeButton("退出",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 取消按钮事件
// Frame
// frame=(Frame)Desktop.getDisktop().getCurrent();
System.exit(1);
}
}).show();

}else{
Log.v("fileName is exist", "file is exist");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivity(intent);
}
} else {
Log.v("fileNameNew==", "file is not read and write");
Toast.makeText(this, "对不起,你没有权限读写该文件!", Toast.LENGTH_SHORT).show();
}

}

[解决办法]
去查api应该有相应的方法

热点排行