This is a continuation post on Android Samba share. This post explains how you can copy files from your Phone directory (i.e. Internal storage or SD card files) to a PC’s shared folder using Samba share.
Make a new layout file in your application named filesfromdirectory.xml and edit it as following :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#414453" android:orientation="vertical" android:padding="10dp" > <EditText android:id="@+id/edtdir" style="@style/myedtbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:hint="LOCAL FILE" /> <EditText android:id="@+id/edtservername" style="@style/myedtbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:hint="IP NAME OR SERVER NAME" /> <EditText android:id="@+id/edtusername" style="@style/myedtbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:hint="USERNAME OR LOGIN ID" /> <EditText android:id="@+id/edtpassword" style="@style/myedtbox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:hint="PASSWORD" /> <Button android:id="@+id/btncopy" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#f7616a" android:text="COPY THIS" android:textColor="#ffffff" android:textStyle="bold" android:typeface="sans" /> <TextView android:id="@+id/txtinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@string/hello_world" android:textColor="#ffffff" android:textSize="16sp" android:textStyle="bold" android:typeface="sans" /> <ProgressBar android:id="@+id/pbbar" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
Make a new class file in your application with name FilesFromDirectory.java.
Declare following variables :
TextView txtinfo; ProgressBar pbbar; EditText edtusername, edtpassword, edtservername, edtdir; Button btncopy;
Initialize these in onCreate method :
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.filesfromdirectory); txtinfo = (TextView) findViewById(R.id.txtinfo); pbbar = (ProgressBar) findViewById(R.id.pbbar); pbbar.setVisibility(View.GONE); edtpassword = (EditText) findViewById(R.id.edtpassword); edtdir = (EditText) findViewById(R.id.edtdir); edtservername = (EditText) findViewById(R.id.edtservername); edtusername = (EditText) findViewById(R.id.edtusername); edtusername.setText("h"); edtpassword.setText("789"); edtservername.setText("192.168.0.100/hh"); btncopy = (Button) findViewById(R.id.btncopy);
Make a new AsyncTask named MyCopy in this class as following :
private class MyCopy extends AsyncTask<String, String, String> { String z = ""; String username = "", password = "", servername = "", filestocopy = ""; @Override protected void onPreExecute() { pbbar.setVisibility(View.VISIBLE); username = edtusername.getText().toString(); password = edtpassword.getText().toString(); servername = "smb://" + edtservername.getText().toString(); filestocopy = edtdir.getText().toString(); } @Override protected void onPostExecute(String r) { txtinfo.setText(r); pbbar.setVisibility(View.GONE); } @Override protected String doInBackground(String... params) { File file = new File(filestocopy); String filename = file.getName(); NtlmPasswordAuthentication auth1 = new NtlmPasswordAuthentication( servername, username, password); try { SmbFile sfile = new SmbFile(servername + "/" + filename, auth1); if (!sfile.exists()) sfile.createNewFile(); sfile.connect(); InputStream in = new FileInputStream(file); SmbFileOutputStream sfos = new SmbFileOutputStream(sfile); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { sfos.write(buf, 0, len); } in.close(); sfos.close(); z = "File copied successfully"; } catch (Exception ex) { z = z + " " + ex.getMessage().toString(); } return z; } }
add onClick listener to your button in onCreate method and call this task inside onCLick method :
btncopy.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MyCopy mycopy = new MyCopy(); mycopy.execute(""); } });
AndroidManisfest.xml permissions :
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
That’s it!!!!