Writing file to Internal Storage
public static void writeFileInternalStorage(String strWrite, Context context,String fileName)
{
try
{
// Check if Storage is Readable
if (isSdReadable()) // isSdReadable()e method is define at bottom of the post
{
String smsfilename = fileName;
FileOutputStream fos = context.openFileOutput(smsfilename,Context.MODE_PRIVATE);
fos.write(strWrite.getBytes());
fos.flush();
fos.close();
}
}
catch (Exception e)
{
// Your Code
}
}
Write File to SD Card
public static void writeFileOnSDCard(String strWrite, Context context,String fileName)
{
try
{
if (isSdReadable()) // isSdReadable()e method is define at bottom of the post
{
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath();
File myFile = new File(fullPath + File.separator + "/"+fileName);
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(strWrite);
myOutWriter.close();
fOut.close();
}
}
catch (Exception e)
{
//do your stuff here
}
}
Read file from Internal Stoarge
public static String readFileFromSDCard(String fileName,Context context)
{
String stringToReturn = "";
try
{
if(isSdReadable()) // isSdReadable()e method is define at bottom of the post
{
String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "/"+fileName;
InputStream inputStream = context.openFileInput(fullPath);
if ( inputStream != null )
{
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null )
{
stringBuilder.append(receiveString);
}
inputStream.close();
stringToReturn = stringBuilder.toString();
}
}
}
catch (FileNotFoundException e)
{
Log.e("TAG", "File not found: " + e.toString());
}
catch (IOException e)
{
Log.e("TAG", "Can not read file: " + e.toString());
}
return stringToReturn;
}
Read File from SD Card
public static String readFileInternalStorage(String fileName, Context context)
{
String stringToReturn = " ";
try
{
if(isSdReadable()) // isSdReadable()e method is define at bottom of the post
{
String sfilename = fileName;
InputStream inputStream = context.openFileInput(sfilename);
if ( inputStream != null )
{
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null )
{
stringBuilder.append(receiveString);
}
inputStream.close();
stringToReturn = stringBuilder.toString();
}
}
}
catch (FileNotFoundException e)
{
Log.e("TAG", "File not found: " + e.toString());
}
catch (IOException e)
{
Log.e("TAG", "Can not read file: " + e.toString());
}
return stringToReturn;
}
Method to Check whether Storage is Readable
public static boolean isSdReadable()
{
boolean mExternalStorageAvailable = false;
try
{
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
// We can read and write the media
mExternalStorageAvailable = true;
Log.i("isSdReadable", "External storage card is readable.");
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
// We can only read the media
Log.i("isSdReadable", "External storage card is readable.");
mExternalStorageAvailable = true;
}
else
{
// Something else is wrong. It may be one of many other
// states, but all we need to know is we can neither read nor
// write
mExternalStorageAvailable = false;
}
} catch (Exception ex)
{
}
return mExternalStorageAvailable;
}
Advance Android Topics
Customizing Toast In Android
Showing Toast for Longer Time
Customizing the Display Time of Toast
Using TimePickerDialog and DatePickerDialog In android
Animating A Button In Android
Populating ListView With DataBase
Customizing Checkboxes In Android
Increasin Size of Checkboxes
Android ProgressBar
Designing For Different Screen Sizes
Handling Keyboard Events
More Android Topics:
Android : Introduction
Eclipse Setup for Android Development
Configuring Eclipse for Android DevelopmentBegging With Android
Creating Your First Android ProjectUnderstanding Android Manifest File of your android app
Working With Layouts
Understanding Layouts in AndroidWorking with Linear Layout (With Example)
Nested Linear Layout (With Example)
Table Layout
Frame Layout(With Example)
Absolute Layout
Grid Layout
Activity
Activity In AndroidActivity Life Cycle
Starting Activity For Result
Sending Data from One Activity to Other in Android
Returning Result from Activity
Working With Views
Using Buttons and EditText in AndroidUsing CheckBoxes in Android
Using AutoCompleteTextView in Android
Grid View
Toast
Customizing Toast In AndroidCustomizing the Display Time of Toast
Customizing Toast At Runtime
Adding Image in Toast
Showing Toast for Longer Time
Dialogs In Android
Working With Alert DialogAdding Radio Buttons In Dialog
Adding Check Boxes In Dialog
Creating Customized Dialogs in Android
Adding EditText in Dialog
Creating Dialog To Collect User Input
DatePicker and TimePickerDialog
Using TimePickerDialog and DatePickerDialog In androidWorking With SMS
How to Send SMS in AndroidHow To Receive SMS
Accessing Inbox In Android
ListView:
Populating ListView With DataBaseMenus In Android
Creating Option MenuCreating Context Menu In Android
TelephonyManager
Using Telephony Manager In AndroidWorking With Incoming Calls
How To Handle Incoming Calls in AndroidHow to Forward an Incoming Call In Android
CALL States In Android
Miscellaneous
Notifications In AndroidHow To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
Storage: Storing Data In Android
Shared Prefferences In Android
SharedPreferences In AndroidFiles: File Handling In Android
Reading and Writing files to Internal StoarageReading and Writing files to SD Card
DataBase : Working With Database
Working With Database in AndroidCreating Table In Android
Inserting, Deleting and Updating Records In Table in Android
How to Create DataBase in Android
Accessing Inbox In Android
why do you need to call isSdReadable() from writeFileInternalStorage()
ReplyDeleteThis is really a very good post
ReplyDeleteAndroid tutorial Read Write text file example kitkat 4.4 with Source code
append not work
ReplyDeleteExcellent and very exciting site. Love to watch. Keep Rocking. here
ReplyDeleteI am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. 10 likes
ReplyDeleteLiên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
chuyến bay từ mỹ về việt nam tháng 1/2021
vé máy bay vietjet từ nhật về việt nam
khi nào có chuyến bay từ đức về việt nam
từ canada về việt nam quá cảnh ở đâu
gia ve may bay vietjet tu han quoc ve viet nam
giá khách sạn cách ly ở hà nội
chi phi ve may bay cho chuyen gia nuoc ngoai