Writing to external storage android

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  String status = Environment.getExternalStorageState();
  if (!status.equals(Environment.MEDIA_MOUNTED)) {

   return;
  }

  File sdcard = Environment.getExternalStorageDirectory();
  FileWriter fileWriter = null;
  FileReader fileReader = null;
  char[] buf = null;
  try {
   File newfile = new File(sdcard.getAbsolutePath() + "/dummy.txt");
   fileWriter = new FileWriter(newfile);
   fileWriter.write("This is a demo");
   fileReader = new FileReader(newfile);
   buf = new char[50];
   fileReader.read(buf);
   ((TextView) findViewById(R.id.tv)).setText(String.valueOf(buf));

  } catch (Exception e) {
   Log.e("PATH ERROR", e.toString());
  }
  finally {
   try {
    fileWriter.close();
   }
   catch (IOException e) {
    e.printStackTrace();
   }
  }

 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}




Ads :
Electronics, Cars, Fashion, Collectibles, Coupons and More Online Shopping | eBay
www.ebay.com | www.ebay.ch | www.ebay.com.my

1 comment:

  1. nice one work , for providing these program codes are more helping to generate android application where requiring for saving a data in external storage, thanks a lot for this.
    __________
    ibm external storage

    ReplyDelete