You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.
Here is a example of saving file in Internal Storage.
package com.exercise.AndroidInternalStorage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AndroidInternalStorageActivity extends Activity {
EditText edFileName, edContent;
Button btnSave;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edFileName = (EditText)findViewById(R.id.filename);
edContent = (EditText)findViewById(R.id.content);
btnSave = (Button)findViewById(R.id.save);
btnSave.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String fileName = edFileName.getText().toString();
String content = edContent.getText().toString();
FileOutputStream fos;
try {
fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
Toast.makeText(
AndroidInternalStorageActivity.this,
fileName + " saved",
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter File Name" />
<EditText
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Content" />
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"/>
</LinearLayout>
Download the files.
next:
- List the saved files in Internal Storage
No comments:
Post a Comment