john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

android write file to sd

todo: button to change colors input with button to change colors textview font?

write file example read file example


android emulator tweak - file write

First always ensure you've got an emulator with the right platform level, it's probably best to use 2.1 for all defaults as it will ensure the highest level of compatibility. Additionally don't forget that after ADT /SDK plugin install:

  1. Eclipse -> Window -> AVD Manager -> New AVD (include a virtual SD card!)
  2. Also, right click on the Project -> Run As -> Run Configuration
  3. Android Application -> Notepadv1 ... Target tab should be the android virtual device with 2.1 API and SD card

package kittyandbear.net;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.graphics.Color;

public class Notepadv1Activity extends Activity 
{
    @Override
    public void onCreate( Bundle savedInstanceState ) 
    {
        super.onCreate( savedInstanceState );
      setContentView( R.layout.main );
      TextView tv = new TextView( this );       // http://developer.android.com/reference/android/widget/TextView.html
      setTextColors( tv );

    if( isExternalStorageWriteable() )
    {
        tv.setText( "I can write a file!" );
    }
    else
    {
        tv.setText( "I can not write a file =[" );
    }

        setContentView( tv );
    }

-   private boolean isExternalStorageWriteable( )
    {
        boolean result = false;
        String state = Environment.getExternalStorageState();

        // http://developer.android.com/reference/android/os/Environment.html
        if( state.equals( Environment.MEDIA_MOUNTED )  &&  !state.equals( Environment.MEDIA_MOUNTED_READ_ONLY ) )
        {       result = true;
        }
        return result;
    }

    private void setTextColors( TextView tv )
    {
        int bgcolor = Color.parseColor( "#D3D3D3" );
        int fgcolor = Color.parseColor( "#000000" );
        tv.setBackgroundColor( bgcolor );
        tv.setTextColor( fgcolor );
    }

} //end class



// import android.view.Menu;    import android.view.MenuItem;


    /*
    @Override
    public boolean onCreateOptionsMenu( Menu menu ) 
    {
      return super.onCreateOptionsMenu( menu );
    }

    @Override
    public boolean onOptionsItemSelected( MenuItem item ) 
    {
      return super.onOptionsItemSelected( item );
    }
*/

  • « android hi
  • command line compilation »

Published

Apr 22, 2012

Category

android

~220 words

Tags

  • android 9
  • file 92
  • java 252
  • sd 1
  • write 10