Androidコンテンツ保存

5262 ワード

1.ファイル
2.sharepreference
3.content provider
1.ファイル
作成したファイルは/data/data/パッケージ名/files/ファイル名
package com.example.file;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class FileService {

	
	private Context context;
	
	public FileService(Context context) {
		super();
		this.context = context;
	}

	/**
	 *     
	 * @param filename
	 * @param filecontent
	 * @throws FileNotFoundException 
	 */
	public void save(String filename, String filecontent) throws FileNotFoundException {
		// TODO Auto-generated method stub
		

			FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);

			try {
				outStream.write(filecontent.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			try {
				outStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	
	public String read(String filename) throws IOException 
	{
		
		FileInputStream inStream = context.openFileInput(filename);
		
		
		//           ,      ,    
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		
		int len =0;
		while(	(len = inStream.read(buffer)) != -1 )
		{
			outStream.write(buffer,0,len);
			
		}
	
		byte[] data = outStream.toByteArray();
inStream.close();
outStream.close();
		String s=new String(data);
		return s;
	}
	
}


呼び出し
			String filename=mEditName.getText().toString();
				String filecontent=mEditContent.getText().toString();
				
				
				FileService service = new FileService(getApplicationContext());
				try {
					service.save(filename,filecontent);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_SHORT);
					e.printStackTrace();
				}
				
			String filename=mEditName.getText().toString();
				FileService service = new FileService(getApplicationContext());

				try {
					String content=service.read(filename);
					mEditContent.setText(content);
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

2.sharepreferenceビジネスクラス
package com.example.service;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.EditTextPreference;

public class PreferService {

	private Context context;
	
	public PreferService( Context c) {
		// TODO Auto-generated method stub
		this.context=c;
		
	}
	
	
	/**
	 *     
	 * @param name
	 * @param age
	 */
	public void save(String name, Integer age) {
		// TODO Auto-generated method stub
		SharedPreferences sh =context.getSharedPreferences("shareprefer", Context.MODE_PRIVATE);//      SharedPreferences
		Editor editor = sh.edit();
		
		editor.putString("name", name);
		editor.putInt("age", age);
		
		editor.commit();
	}

	/**
	 *     
	 * @return
	 */
	public Map<String ,String> getPreference()
	{
		Map<String ,String> map =new  HashMap<String ,String>();
		SharedPreferences sh = context.getSharedPreferences("shareprefer", Context.MODE_PRIVATE);
		map.put("name", sh.getString("name", ""));
		map.put("age", String.valueOf(sh.getInt("age", 0)));
		return map;
	}
}
activityで呼び出す
package com.example.a;

import java.util.Map;

import com.example.service.PreferService;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button m_bt1;
	private EditText m_edt1;
	private EditText m_edt2;
	
	private PreferService service;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_bt1 = (Button)findViewById(R.id.button1);
        m_edt1 =(EditText)findViewById(R.id.editText1);
        m_edt2 =(EditText)findViewById(R.id.editText2);
        
        
        service =new PreferService(this);//         
        //service =new PreferService(getApplicationContext());
        Map<String , String> map = service.getPreference();
        
        m_edt1.setText(map.get("name"));
        m_edt2.setText(map.get("age"));
        
        m_bt1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				service.save(m_edt1.getText().toString(),Integer.parseInt(m_edt2.getText().toString()));
				
				Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();
				
			}
		});

    }

    }