[英]open failed: ENOENT (No such file or directory) internal storage
I have a little bit of trouble finding my error in my Android application. 我在Android应用程序中发现错误时有些麻烦。
The target of my "ResultActivity" is, to create a file in the internal storage and then download a file from my server (via FTP) and then call the next Activity (GiftActivity). 我的“ ResultActivity”的目标是在内部存储器中创建一个文件,然后从我的服务器下载文件(通过FTP),然后调用下一个活动(GiftActivity)。
But when the next Activity tries to access these files, I get the mentioned error. 但是,当下一个Activity尝试访问这些文件时,出现上述错误。 As far nothing new, but the interesting part is, that if I kill the application by hand and restart it, it works like a charm the next time.
到目前为止,没有什么新鲜的,但是有趣的是,如果我手动杀死该应用程序并重新启动它,则下次它会像一个符咒一样工作。
Here's my code: 这是我的代码:
ResultActivity.java: ResultActivity.java:
//...
ImageButton download = (ImageButton) findViewById(R.id.downloadCode);
download.setBackgroundDrawable(null);
assert download != null;
download.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View v) {
new AsyncTask < Integer, Void, Void > () {@
Override
protected Void doInBackground(Integer...params) {
try {
JSch jsch2 = new JSch();
session = jsch2.getSession("xy", "1.2.3.4", 22);
session.setPassword("xyz123");
//Set FTP Settings
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
//Disables Fingerprint checking!
session.setConfig(config);
//load config to session
session.connect();
//connect to the session
channel = session.openChannel("sftp");
channel.connect();
//open an SFTP Channel
channelSftp = (ChannelSftp) channel;
File checkExistance = getBaseContext().getFileStreamPath("code.txt");
if (checkExistance.exists()) {
getBaseContext().deleteFile("code.txt");
getBaseContext().deleteFile("usage.txt");
}
File code = new File(getFilesDir().getAbsolutePath(), "code.txt");
File usage = new File(getFilesDir().getAbsolutePath(), "usage.txt");
code.createNewFile();
usage.createNewFile();
channelSftp.get("/home/code.txt", getFilesDir() + "/code.txt");
channelSftp.get("/home/usage.txt", getFilesDir() + "/usage.txt");
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
Intent intent = new Intent(ResultActivity.this, GiftActivity.class);
ResultActivity.this.startActivity(intent);
ResultActivity.this.finish();
}
});
GiftActivity.java: GiftActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gift);
TextView actualCode = (TextView) findViewById(R.id.actualCode);
TextView actualUsage = (TextView) findViewById(R.id.usageCode);
String code = "";
String usage = "";
try {
FileInputStream fis = getBaseContext().openFileInput("code.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
code = sb.toString();
} catch (IOException e) {
e.printStackTrace();
code="error1";
}
try {
FileInputStream fis = getBaseContext().openFileInput("usage.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
usage = sb.toString();
} catch (IOException e) {
e.printStackTrace();
usage="error2";
}
actualCode.setText(code);
actualUsage.setText("you got a gift for " + usage + " congratulation!");
}
I fixed it by replacing: 我通过替换来修复它:
File code = new File(getFilesDir().getAbsolutePath(), "code.txt");
File usage = new File(getFilesDir().getAbsolutePath(), "usage.txt");
code.createNewFile();
usage.createNewFile();
With: 带有:
File code = new File(getBaseContext().getFilesDir(), "code.txt");
File usage = new File(getBaseContext().getFilesDir(), "usage.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.