classExternalData

Last-modified: 2015-03-18 (水) 12:37:37
public class ExternalData extends Activity implements OnItemSelectedListener, OnClickListener{

	private TextView canWrite, canRead;
	private String state;
	boolean canW, canR;
	Spinner spinner;
	String[] paths = {"Music", "Picture", "Download"};
	File path = null;
	File file = null;
	EditText saveFile;
	Button confirm, save;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.externaldata);
		canWrite = (TextView) findViewById (R.id.tvCanWrite);
		canRead = (TextView) findViewById (R.id.tvCanRead);
		confirm = (Button) findViewById (R.id.bConfirmSaveAs);
		save = (Button) findViewById (R.id.bSaveFile);
		saveFile = (EditText) findViewById (R.id.etSaveAs);
		confirm.setOnClickListener(this);
		save.setOnClickListener(this);

		checkState();

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(ExternalData.this, android.R.layout.simple_list_item_1, paths);
		adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		spinner = (Spinner) findViewById (R.id.spinner1);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}

	private void checkState() {
		// TODO 自動生成されたメソッド・スタブ
		state = Environment.getExternalStorageState();
		if(state.equals(Environment.MEDIA_MOUNTED)){
			// read and write
			canWrite.setText("true");
			canRead.setText("true");
			canW = canR = true;
		} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
			// read but can't write
			canWrite.setText("true");
			canRead.setText("true");
			canW = false;
			canR = false;
		} else{
			canWrite.setText("false");
			canRead.setText("false");
			canW = canR = false;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		// TODO 自動生成されたメソッド・スタブ
		int position = spinner.getSelectedItemPosition();
		switch(position){
		case 0:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
			break;
		case 1:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
			break;
		case 2:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){

		case R.id.bSaveFile:
			String f = saveFile.getText().toString();
			file = new File(path, f + ".png");
			checkState();
			if(canW == canR == true){
				path.mkdirs();

				try {
					InputStream is = getResources().openRawResource(R.drawable.kuma_64);
					OutputStream os = new FileOutputStream(file);
					byte[] data = new byte[is.available()];
					is.read(data);
					os.write(data);
					is.close();
					os.close();

					Toast t = Toast.makeText(ExternalData.this, "File has been Saved", Toast.LENGTH_LONG);
					t.show();
					// update files the user to use
					MediaScannerConnection.scanFile(ExternalData.this,
							new String[]{file.toString()},
							null,
							new MediaScannerConnection.OnScanCompletedListener() {

								@Override
								public void onScanCompleted(String path, Uri uri) {
									// TODO 自動生成されたメソッド・スタブ
									Toast t = Toast.makeText(ExternalData.this, "scan complete", Toast.LENGTH_SHORT);
									t.show();
								}
							});

				} catch (FileNotFoundException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				} catch (IOException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
			}

			break;
		case R.id.bConfirmSaveAs:
			save.setVisibility(View.VISIBLE);
			break;
		}
	}
}




~
~<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tvCanWrite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <TextView
        android:id="@+id/tvCanRead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
~</LinearLayout>