Hot Cool Stud
Hot Cool Stud

Reputation: 1205

SMB getinputstream application crashes

I am facing the same issue which was faced on link and was not answered properly. I am using code below.

 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();
                        }
                        

                    
                });

When i comment sfile.getInputStream(); application works perfect but with getinputstream application without any exception in try catch block. As the above mentioned question is not answered so i am putting this question again.

edit

Now i tried with Async Task

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;
    }

With Async Task now SmbFileOutputStream is working but still showing Fatal Error with getinputstream.

Edit

Found the reason behind Appcrash is networkmainthread exception. Now kindly guide to resolve it.

Upvotes: 4

Views: 282

Answers (1)

cwbowron
cwbowron

Reputation: 1025

I think the NtlmPasswordAuthentication call is wrong. It is expecting the string to be host;user:password. In your case that should be 192.168.1.12;myusername:mypassword. You could also use the 3 parameter version of the constructor: NtlmPasswordAuthentication("192.168.1.12","myusername","mypassword").

Upvotes: 2

Related Questions