วันพฤหัสบดีที่ 12 กรกฎาคม พ.ศ. 2555

Android SD Card

--> Android SD Card, Android write and save file

In addition to the Android-specific data storage methods mentioned previously, the standard java.io.File Java package is available, too.This provides for flat file manipulation, such as FileInputStream, FileOutputStream, InputStream, and OutputStream.An example is reading from and writing to a file:

FileInputStream fis = openFileInput("myfile.txt");
FileOutputStream fos = openFileOutput("myfile.txt", Context.MODE_WORLD_WRITABLE);

Another example is saving the bitmap camera picture to a PNG file, as follows:
Bitmap takenPicture;

FileOutputStream out = openFileOutput("mypic.png", Context.MODE_WORLD_WRITEABLE);
takenPicture.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();

The files in the resources directories can also be opened. For example, to open myrawfile.txt located in the res/raw folder, use the following:
InputStream is = this.getResource().openRawResource(R.raw.myrawfile.txt);


Android Permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


Android SD Card Example Code
AndroidSDcardActivity.java Code
package android.code.sdcard;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidSDcardActivity extends Activity {
   
      File file;
      FileOutputStream fos;
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btnSave = (Button) findViewById(R.id.button1);
       
        btnSave.setOnClickListener(new Button.OnClickListener(){
           
            public void onClick(View arg0) {
                   
              try {                            
                  String filename = "SDcard.txt";
                    file = new File(Environment.getExternalStorageDirectory(),
                              filename);                          
                 
                        byte[] data = new String("data to write to file").getBytes();                
                       
                        fos = new FileOutputStream(file);
                        fos.write(data);                                 
                        fos.flush();
                        fos.close();
              
                        Toast.makeText(getBaseContext(),
                              "Done writing SDcard.txt ",Toast.LENGTH_SHORT).show();
                          
                  } catch (FileNotFoundException e) {
                        // handle exception          
                  } catch (IOException e) {
                        // handle exception
                  }
      
            }
        });
      }
   
}

main.xml Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#9cf9f1"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Android SDcard"
        android:textColor="#FF0000"
        android:textSize="30dp" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Create and Save file to SD card" />

</LinearLayout>

AndroidManifest.xml  Code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.code.sdcard"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidSDcardActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
   
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

</manifest>




Download Android SD card Example Code -->

More info
Android SD card Files


Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android