专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Google技术

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

发布时间:2011-07-03 07:10:17 文章来源:www.iduyao.cn 采编人员:星星草
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应该有相应的方法
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: