【发布时间】:2018-02-06 13:03:32
【问题描述】:
我面临着与link 相同的问题,但没有得到正确的回答。我正在使用下面的代码。
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
String url = "smb://DHARMU-PC/" + sharedFolder + "/new plugin.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null);
SmbFile sfile = new SmbFile(url,auth);
if (sfile!=null)
{
Toast.makeText(Main2Activity.this,"Not", Toast.LENGTH_SHORT).show();
sfile.getInputStream();
}
else
{
Toast.makeText(Main2Activity.this, "Succsess", Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
当我评论 sfile.getInputStream(); 应用程序运行完美,但 getinputstream 应用程序在 try catch 块中没有任何异常。由于上面提到的问题没有得到回答,所以我再次提出这个问题。
编辑
现在我尝试了异步任务
class CopySMBFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
try {
String fileContent = "This is a test file";
try{
String user = "myusername:mypassword";
System.out.println("User: " + user);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://192.168.1.12/k/temp/A.txt";
System.out.println("Path: " +path);
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(fileContent.getBytes());
System.out.println("Finished attempt to write file");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Connected", e.getMessage());
}
return null;
}
现在使用异步任务,SmbFileOutputStream 正在工作,但仍然显示 getinputstream 的致命错误。
编辑
发现Appcrash背后的原因是networkmainthread异常。现在请指导解决它。
【问题讨论】:
-
"Android smb getinputstream 应用程序崩溃" -- 使用 LogCat 检查与您的崩溃相关的 Java 堆栈跟踪:stackoverflow.com/questions/23353173/…
-
您是否尝试关闭您的信息流?我只是好奇...
sfile.getInputStream().close();?