Sunday, October 16, 2011

List the saved files in Internal Storage

To list the saved files in Internal Storage, the method fileList() can be used. It returns an array of strings naming the private files associated with this Context's application package.

It's a example, in ShowSavedFiles() method, get the saved file name and list in ListView.

List the saved files 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.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidInternalStorageActivity extends Activity {

EditText edFileName, edContent;
Button btnSave;
ListView listSavedFiles;

String[] SavedFiles;

/** 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);
listSavedFiles = (ListView)findViewById(R.id.list);

ShowSavedFiles();

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();
}

ShowSavedFiles();

}});

}

void ShowSavedFiles(){
SavedFiles = getApplicationContext().fileList();
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
SavedFiles);

listSavedFiles.setAdapter(adapter);
}
}


<?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"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Download the files.

next:
- Read file from Internal Storage via FileInputStream



9 comments:

Karthik.K said...

Hi,
How do I download a file directly into the private folder?
Also, how do I access it from there after downloading? Kindly help! Urgent!

andoy said...

How to delete the saved file?

Android Er said...

hello andoy,

Please refer Delete file in Internal Storage

Tse said...

Hello.

This tutorial is really exciting. I'm newbie. I have a question. How save 3 or 4 edittext in one list row? fileList() method saved only one fos(FILEOUTPUTSTREAM) file. How solve this problem? please.

Thank you.

Andr.oid Eric said...

In my understand, you can retrieve the text from the various edittext, and add them together to have one String.

ex.
String result = edittext1.getText().toString + edittext2.getText().toString +
...;

oyu99 said...

Hello.
I put in 2 items and 1 image in internal storage list row so how solve this problem. could u create another example please. it's important to me.
please!!!

D Ward said...

I would really like to thank you. You just don't know how much you really helped me. Thank you so so much. I appreciate your help. Thank you, thank you, and thank you.

Hanisha Sachdeva said...

how to open the saved files

Andr.oid Eric said...

hello Hanisha Sachdeva,

Refer to the next post Read file from Internal Storage via FileInputStream.