Delete file in Internal Storage
Here method deleteFile() delete the given file associated with this Context's application package.
![Delete file in Internal Storage Delete file in Internal Storage](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5aySvpvTxvMQPY4w7cRXi6lNXSf5dtCUzL643aNaZLX0Tw05vsLTBEnR8CZeFplO2AfHtw76BodPDqAF08l0Q0qrxPg-R0_z8Xe2SobzlUF2pbG0DVtsCKIu9CwKIYdHOwhhGpDuTPUyx/s400/Internal+Storage_deletefile_01.png)
![Delete file in Internal Storage Delete file in Internal Storage](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm02Mxhz1V0lkfzS3tpUuPGdNJ4CHuBBIsNxIhOZ3ac8Ozj0VofmdjMfkbh3jXoCxKtzOpW8OHBgWUOUsEz9b4TsCvUhJ-6l606Cw1okp8wBqlGBWYIVmx1G4wwizAITXiDd4pZsNJ5ojS/s400/Internal+Storage_deletefile_02.png)
Download the files.
![Delete file in Internal Storage Delete file in Internal Storage](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj5aySvpvTxvMQPY4w7cRXi6lNXSf5dtCUzL643aNaZLX0Tw05vsLTBEnR8CZeFplO2AfHtw76BodPDqAF08l0Q0qrxPg-R0_z8Xe2SobzlUF2pbG0DVtsCKIu9CwKIYdHOwhhGpDuTPUyx/s400/Internal+Storage_deletefile_01.png)
![Delete file in Internal Storage Delete file in Internal Storage](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm02Mxhz1V0lkfzS3tpUuPGdNJ4CHuBBIsNxIhOZ3ac8Ozj0VofmdjMfkbh3jXoCxKtzOpW8OHBgWUOUsEz9b4TsCvUhJ-6l606Cw1okp8wBqlGBWYIVmx1G4wwizAITXiDd4pZsNJ5ojS/s400/Internal+Storage_deletefile_02.png)
package com.exercise.AndroidInternalStorage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
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();
}});
listSavedFiles.setOnItemClickListener(listSavedFilesOnItemClickListener);
}
void ShowSavedFiles(){
SavedFiles = getApplicationContext().fileList();
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
SavedFiles);
listSavedFiles.setAdapter(adapter);
}
OnItemClickListener listSavedFilesOnItemClickListener
= new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String clickedFile = (String) parent.getItemAtPosition(position);
OpenFileDialog(clickedFile);
}
};
void OpenFileDialog(final String file){
//Read file in Internal Storage
FileInputStream fis;
String content = "";
try {
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Create a custom Dialog
AlertDialog.Builder fileDialog
= new AlertDialog.Builder(AndroidInternalStorageActivity.this);
fileDialog.setTitle(file);
TextView textContent = new TextView(AndroidInternalStorageActivity.this);
textContent.setText(content);
LayoutParams textViewLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textContent.setLayoutParams(textViewLayoutParams);
fileDialog.setView(textContent);
fileDialog.setPositiveButton("OK", null);
//Delete file in Internal Storage
OnClickListener DeleteListener = new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
deleteFile(file);
Toast.makeText(
AndroidInternalStorageActivity.this,
file + " deleted",
Toast.LENGTH_LONG).show();
ShowSavedFiles();
}
};
fileDialog.setNeutralButton("DELETE", DeleteListener);
fileDialog.show();
}
}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhppKQNzeFV8fQqgdIedx7YJI7x8om19IFIJlsMFgFFQi9tx5FLRR31MBr6S1mblo33N4yg_e5p3XSrNAZdPbNvf7UOKvOZ_1DaVxvmRz4hgnO95juJRaxklaDl945G4c3brulVNQBsl-TJ/s200/icons_download.gif)
No comments:
Post a Comment