We already know how to save files and read files in
Android. But here I am going to tell you about how a file created by one application is shared with another application. The file created by the first application is saved in the data folder, as you all know that the data storage is private to the application.
The task I am going to tell you is I have two application, the first application has an edittext, button and a text view on clicking on the button the application will save the text which is present in the edittext to a file in the data storage and show the status in the text view that is whether the file is created or not.
The second application has also three controls they are two text view and one button. On clicking in the button the contents from the file which are created by the first application are loaded into the first text view and the status will be shown in the next text view.
Create the first applications its name is ShareAppOne
Here I am using an Android studio for creating the two applications. Create a blank activity and its layout. Suppose the layout file is activity_main.xml. which will contain one EditText one Button and a TextView. Here is the XML file.
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
-
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/editText"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="38dp" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save to File"
- android:id="@+id/button"
- android:layout_below="@+id/editText"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="54dp" />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Status"
- android:id="@+id/statusView"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Now declare two private variables in the MainActivity,
- private EditText editText = null;
- private TextView statusText = null;
And in the onCreate() please initialize these variables and the buttons.
- editText = (EditText) findViewById(R.id.editText);
- statusText = (TextView) findViewById(R.id.statusView);
- Button saveFile = (Button) findViewById(R.id.button);
Now we need to do the main things like we need to do save the contents to file in the data folder on clicking on the button. Please see the code below.
- private void saveFile(){
- File file = null;
- FileOutputStream fileOutputStream = null;
-
- file = getFilesDir();
- try {
- fileOutputStream = openFileOutput("app1.txt", MODE_PRIVATE);
- fileOutputStream.write(editText.getText().toString().getBytes());
- statusText.setTextColor(Color.GREEN);
- statusText.setText("File write "+file.getAbsolutePath());
- fileOutputStream.close();
- } catch (FileNotFoundException e) {
- statusText.setTextColor(Color.RED);
- statusText.setText("Error " + e.getMessage());
- } catch (IOException e) {
- statusText.setTextColor(Color.RED);
- statusText.setText("Error " + e.getMessage());
- }
- }
The above method will save the file in the data folder of this application.
Now the main thing here is each applications has its own application id and the process id which will be assigned by the Android operating system while the time of installing the application. The application id will be unique. So we need to set a shared application id for this two application we are going to create. That we need to set in the Manifest file. Like the following,
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- android:sharedUserId="com.negociosit.shareApp"
- package="sample.negociosit.com.shareappone" >
Create the second application named ShareAppTwo
This application also contains two text views and a button. The first text view will show the contents of the file on clicking in the button and the second textview will display the status.
Please see the xml file for the layout,
- <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
-
- <TextView android:text="@string/hello_world"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="63dp" />
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Load Text"
- android:id="@+id/loadButton"
- android:layout_below="@+id/textView"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="50dp" />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Status"
- android:id="@+id/statusText"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
-
- </RelativeLayout>
Now initialize these controls in the onCreate() of your MainActivity,
- textView = (TextView) findViewById(R.id.textView);
- statusTextView = (TextView) findViewById(R.id.statusText);
- Button loadButton = (Button) findViewById(R.id.loadButton);
On Clicking on the loadButton we need to show the file contents for that first we need to check whether the first application exists or not by using the following code.
- PackageManager packageManager = getPackageManager();
- try {
- ApplicationInfo applicationInfo = packageManager.getApplicationInfo("sample.negociosit.com.shareappone",PackageManager.GET_META_DATA);
-
- readFile(applicationInfo.dataDir +"/files/app1.txt");
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
In the Application info object we need to pass the package name of the application which we want to get info. If that package is not existing then it will get an exception like NameNotFoundException. Here we need to put the ShareAppOne package name because we need to read a file from that application’s private storage. If that package exists then we need to read the contents of the file using the following code.
- private void readFile(String filePath){
- FileInputStream fileInputStream = null;
- try {
- fileInputStream = new FileInputStream(new File(filePath));
- int read = -1;
- StringBuffer stringBuffer = new StringBuffer();
- while((read = fileInputStream.read())!=-1){
- stringBuffer.append((char)read);
- }
- textView.setText(stringBuffer.toString());
- statusTextView.setTextColor(Color.GREEN);
- statusTextView.setText("File loaded successfully");
- } catch (FileNotFoundException e) {
- statusTextView.setTextColor(Color.RED);
- statusTextView.setText("Error " + e.getMessage());
- } catch (IOException e) {
- statusTextView.setTextColor(Color.RED);
- statusTextView.setText("Error "+e.getMessage());
- }
- }
This will display the file contents in the TextView if no error occurs in reading the file. Please see the screenshots also for the two applications.
Happy coding! Cheers!
Read more articles on Android