stuart-d2
3/23/2014 - 6:15 PM

Read|Write ExternalStorage Ensure AndroidManifest PERMISSION set too.

Read|Write ExternalStorage Ensure AndroidManifest PERMISSION set too.

package course.examples.DataManagement.FileExternal;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import course.examples.Files.FileWriteAndRead.R;

public class ExternalFileWriteReadActivity extends Activity {
	private final String fileName = "painter.png";
	private String TAG = "ExternalFileWriteReadActivity";

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

//Check the external storage to ensure media mounted, readable and writable.  
		if (Environment.MEDIA_MOUNTED.equals(Environment
				.getExternalStorageState())) {

//Get directory of resources -- here, where pictures are normally stored.  
//New file object created pointing within the directory.
			File outFile = new File(
					getExternalFilesDir(Environment.DIRECTORY_PICTURES),
					fileName);

//Checks whether file actually exists on external memory.
//Copy image data to memory method called.   
			if (!outFile.exists())
				copyImageToMemory(outFile);

//Last step (not linear code) ImageView reference inset with the image URI of the file
//--that was created in external memory.  
			ImageView imageview = (ImageView) findViewById(R.id.image);
			imageview.setImageURI(Uri.parse("file://" + outFile.getAbsolutePath()));
		
		}
	}

//CopyImageToMemory Method.
//Creates a new output stream used to write to a file on ext. memory.
//Creates a new input stream to read image data from the res/raw directory.
//Copies data from input stream to the output stream.  
	private void copyImageToMemory(File outFile) {
		try {

			BufferedOutputStream os = new BufferedOutputStream(
					new FileOutputStream(outFile));

			BufferedInputStream is = new BufferedInputStream(getResources()
					.openRawResource(R.raw.painter));

			copy(is, os);

		} catch (FileNotFoundException e) {
			Log.e(TAG, "FileNotFoundException");
		}
	}

	private void copy(InputStream is, OutputStream os) {
		final byte[] buf = new byte[1024];
		int numBytes;
		try {
			while (-1 != (numBytes = is.read(buf))) {
				os.write(buf, 0, numBytes);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
				os.close();
			} catch (IOException e) {
				Log.e(TAG, "IOException");

			}
		}
	}
}
================================================================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="course.examples.Files.FileWriteAndRead"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="course.examples.DataManagement.FileExternal.ExternalFileWriteReadActivity"
                  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"></uses-permission>
</manifest>
===================================================================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/image"
        android:contentDescription="@string/android_painter_desc"
        android:layout_centerInParent="true"/>

</RelativeLayout>