In this article we are learning about save data to file in sdcard.we can save any kind of data or images in sdcard by using below concepts
* FileOutputStream
* OutputStreamWriter
First we have to give permission for accessing SDCARD for that
1) AndroidManifest.xml
add the below permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2) MainActivity.java
add the below code in java file
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
try {
String scaneddata="save data to file in sdcard";
String dir = Environment.getExternalStorageDirectory()+File.separator+"sample";
File folder = new File(dir);
folder.mkdirs();
File folderpath = new File(folder+File.separator+"sample.txt");
folderpath.createNewFile();
FileOutputStream fOut = new FileOutputStream(folderpath);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(scaneddata);
myOutWriter.close();
fOut.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
3)Demo
Finally,Run your application
OUTPUT:
sample.txt file with data will be created in sample folder in sdcard of device
see the created sample.txt file in path: sdcard/sample/sample.txt
see the saved data in sample.txt file
* FileOutputStream
* OutputStreamWriter
First we have to give permission for accessing SDCARD for that
1) AndroidManifest.xml
add the below permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.forblog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
add the below code in java file
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
try {
String scaneddata="save data to file in sdcard";
String dir = Environment.getExternalStorageDirectory()+File.separator+"sample";
File folder = new File(dir);
folder.mkdirs();
File folderpath = new File(folder+File.separator+"sample.txt");
folderpath.createNewFile();
FileOutputStream fOut = new FileOutputStream(folderpath);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(scaneddata);
myOutWriter.close();
fOut.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
3)Demo
Finally,Run your application
OUTPUT:
sample.txt file with data will be created in sample folder in sdcard of device
see the created sample.txt file in path: sdcard/sample/sample.txt
see the saved data in sample.txt file