Android Samba Sharing – Creating Files

This is an application example in Android showing usage of Samba server library for creating files in remote computer. Download the JCIFS 1.3.17 jar library file and add it to your application.

Make a new folder in your D or E drive and share it in your network. Note the IP ADDRESS of your PC.

For example my PC’s IP address is 192.168.0.100 and I made a folder named HH > Hitya then my Samba share path is
smb://192.168.0.100/hh/hitya/

Open your activity_main.xml file and edit it as following :

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.example.androidsambashare.MainActivity" >

 <TextView
 android:id="@+id/txtinfo"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_world"
 android:textSize="25sp"
 android:textStyle="bold|italic"
 android:typeface="sans" />

 <ProgressBar
 android:id="@+id/pbbar"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

</LinearLayout>

android samba share copying files 1

Edit your MainActivity.java as following :


package com.example.androidsambashare;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

	TextView txtinfo;
	ProgressBar pbbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		txtinfo = (TextView) findViewById(R.id.txtinfo);
		pbbar = (ProgressBar) findViewById(R.id.pbbar);
		pbbar.setVisibility(View.GONE);
		
		MyCopy my = new MyCopy();
		my.execute("");
	}

	
	private class MyCopy extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			pbbar.setVisibility(View.VISIBLE);

		}

		@Override
		protected void onPostExecute(String r) {
			txtinfo.setText(r);
			pbbar.setVisibility(View.GONE);
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			String z = "";
			try {

				for (int y = 0; y < 100; y++) {
					String url = "smb://192.168.0.100/hh/hitya/"
							+ String.valueOf(y) + ".txt";

					NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
							null, "h", "789");
					SmbFile sfile = new SmbFile(url, auth);

					if (!sfile.exists()) {
						sfile.createNewFile();
						z = "Created the file for you!!!!";
					} else
						z = "Already exists at the specified location!!!!";
				}
			} catch (Exception ex) {
				// TODO: handle exception
				z = ex.getMessage().toString();
			}
			return z;
		}
	}
}

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" />

This post shows how to Copy files from Phone directory to a share folder!!!


3 thoughts on “Android Samba Sharing – Creating Files”

  1. Pingback: Android Samba Sharing - Copying files from Phone directory • ParallelCodes

  2. hi, i read your article, i need your support, i need to connect my app made in flutter with smb protocol can you do it? how much could it cost?

    contact me thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.