请帮忙....
我已经为 Android 2.2 创建了一个应用程序,它创建一个文本文件并保存到 sd 卡。在另一个活动中,它读取此文件并存储到数组中。该应用程序在 2.2 上运行良好,在我的 samsung nexus 与 4.1 上运行良好。(ICS)但是我刚刚更新到果冻豆(4.2)并且应用程序现在在尝试打开文件时崩溃。我已经阅读了关于在 4.2 的清单中添加读取权限的文档,并添加了这个,但应用程序仍然崩溃。没有日志猫,因为它在模拟器上运行良好,但在我的手机上却不行。
4.2 或打开文件以进行读取的不同方式是否需要任何特殊权限?
这是我用于创建目录和写入文件的代码...
//Create Reports Folder...........................................
File directory = new File
(Environment.getExternalStorageDirectory().getPath()+"/PatRecords");
if (!directory.exists()) {
directory.mkdir();
Toast.makeText(this, "NEW FOLDER CREATED", Toast.LENGTH_SHORT).show();
}
//If Sd card available, and writable with no errors, then write file.........
if ((mExternalStorageAvailable = true)&(mExternalStorageWriteable = true)&(Error==0))
{
try {
File ReportFile = new File(directory.getPath()+"/"+FileName$);
ReportFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(ReportFile);
OutputStreamWriter OutWriter = new OutputStreamWriter(fOut);
OutWriter.write(Tester$+"\r\n");
OutWriter.write(Company$+"\r\n");
OutWriter.write(Customer$+"\r\n");
OutWriter.write(Address1$+"\r\n");
OutWriter.write(Address2$+"\r\n");
OutWriter.write(PCode$+"\r\n");
OutWriter.write(Instr$+"\r\n");
OutWriter.write(Serial$+"\r\n");
OutWriter.close();
fOut.close();
Toast.makeText(this, "NEW FILE CREATED", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}//End of try/catch
}//End Write.....
这是用于阅读的代码....在 4.1 之前可以正常工作,但不能在 4.2...
FilePath=getIntent().getStringExtra("FileToOpen");
//Open File and read first all data.......
try {
BufferedReader br = new BufferedReader(new FileReader(FilePath));
Tester$ = br.readLine();
Company$ = br.readLine();
Customer$ = br.readLine();
Address1$ = br.readLine();
Address2$ = br.readLine();
PCode$ = br.readLine();
Instr$ = br.readLine();
InstrSerial$ = br.readLine();
NumOfTests$ = br.readLine();
br.close();
Toast.makeText(this, "FILE LOADED OK.", Toast.LENGTH_SHORT).show();
n = n+1; //Increment n for next test number....
}
catch (IOException e) {
//You'll need to add proper error handling here
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
非常感谢任何帮助...感谢您的关注