ºÇ½ª¹¹¿·¡§
t_yokky2002 2011ǯ12·î08Æü(ÌÚ) 14:29:07ÍúÎò
- openFileInput¡¦openFileOutput¤ò»ÈÍѤ·¤Æ¡¢¥Õ¥¡¥¤¥ë¤ËÂФ·¤ÆÆɤ߽ñ¤¤ò¹Ô¤¤¤Þ¤¹¡£
¤³¤ÎAPI¤Ï¡¢Context¥¯¥é¥¹¤Ë¤è¤êÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹(Activity¥¯¥é¥¹¤ÏContext¤ò·Ñ¾µ)¡£
public abstract class Context { public abstract FileInputStream openFileInput(String name); public abstract FileOutputStream openFileOutput(String name, int mode); }
- ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¥Ç¡¼¥¿ÊݸÎΰè¤Ï¡¢¼¡¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ë¤Ê¤ê¤Þ¤¹¡£
/data/data/[package name]/files/
- ¥Õ¥¡¥¤¥ë̾¤Ë¡¢¥»¥Ñ¥ì¡¼¥¿Ê¸»ú¡Ê/¡Ë¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¤È¡¢IllegalArgumentException¤Çcontains a path separator¤Î¥¨¥é¡¼¤¬È¯À¸¤·¤Þ¤¹¡£
- openFileOutput(String name, int mode)¤ò¼Â¹Ô¤·¡¢FileOutputStream¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
- Âè1°ú¿ô: ¥Õ¥¡¥¤¥ë̾
- Âè2°ú¿ô: ¥Õ¥¡¥¤¥ëºîÀ®¥â¡¼¥É(Ä̾ï¤Ï¡¢MODE_PRIVATE¤ò»ÈÍÑ)
¥â¡¼¥É | ÍÑÅÓ |
---|---|
MODE_PRIVATE | ¤³¤Î¥¢¥×¥ê¤«¤é¤Î¤ß»ÈÍѲÄǽ¤Ê¥â¡¼¥É |
MODE_APPEND | Äɵ¥â¡¼¥É |
MODE_WORLD_READABLE | ¾¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤«¤é¤âÆɤ߹þ¤ß²Äǽ¤Ê¥â¡¼¥É |
MODE_WORLD_WRITEABLE | ¾¤Î¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤«¤é¤â½ñ¤¹þ¤ß²Äǽ¤Ê¥â¡¼¥É |
public class SampleActivity extends Activity { private String fileName = "sample.txt"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // ¥¹¥È¥ê¡¼¥à¤ò³«¤¯ FileOutputStream output = openFileOutput(fileName, MODE_PRIVATE); // ½ñ¤¹þ¤ß output.write(str.getBytes()); // ¥¹¥È¥ê¡¼¥à¤òÊĤ¸¤ë output.close(); } catch (IOException e) { e.printStackTrace(); } } }
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.sample); try { OutputStream out = openFileOutput("hoge.png", MODE_PRIVATE); bm.compress(Bitmap.CompressFormat.PNG, 100, out); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
°ú¿ô | ÀâÌÀ |
---|---|
format | Bitmap.CompressFormat.JPEG¤Þ¤¿¤ÏBitmap.CompressFormat.PNG |
quality | 0¤«¤é100¤ÎÃͤò»ØÄê¡ÊÉáÄ̤Ï100¡©¡Ë |
stream | °µ½Ì¤·¤¿¥Ó¥Ã¥È¥¹¥È¥ê¡¼¥à¤òή¤·¹þ¤àÀè¤ò»ØÄê |
¥Õ¥¡¥¤¥ë(sample.txt)¤òÆɤ߽Ф·, ¥Æ¥¥¹¥È¥Ó¥å¡¼¤Ëɽ¼¨¤¹¤ë¡£
public class SampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView text = (TextView)findViewById(R.id.textview_id); BufferedReader in = null; try { FileInputStream file = openFileInput("sample.txt"); in = new BufferedReader(new InputStreamReader(file)); text.setText(in.readLine()); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
¡Öread()¥á¥½¥Ã¥É¡×¤Ï¡¢¸Æ¤Ó½Ð¤µ¤ì¤ëÅ٤˥ե¡¥¤¥ë¤ä¡¦¥Í¥Ã¥È¥ï¡¼¥¯¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤¿¤á¡¢¸úΨ¤¬¤è¤¯¤¢¤ê¤Þ¤»¤ó¡£
¥Ð¥Ã¥Õ¥¡µ¡Ç½¤ò»ý¤ÄÆþÎÏ¥¹¥È¥ê¡¼¥à¤Ç¡¢°ìÅ٤Υ¢¥¯¥»¥¹¤Ç¿¤¯¤Î¥Ç¡¼¥¿¤ò¥Ð¥Ã¥Õ¥¡¤Ë³ÊǼ¤¹¤ë¤³¤È¤Ë¤è¤Ã¤Æ¡¢½èÍý¸úΨ¤¬Îɤ¤¡£
public class Main2Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView) findViewById(R.id.image); Bitmap bm = null; try { FileInputStream in = openFileInput("hoge.png"); BufferedInputStream binput = new BufferedInputStream(in); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] w = new byte[1024]; while (binput.read(w) >= 0) { out.write(w, 0, 1024); } byte[] byteData = out.toByteArray(); bm = BitmapFactory.decodeByteArray(byteData, 0, byteData.length); in.close(); binput.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } imageView.setImageBitmap(bm); } }
»²¹Í¤Þ¤Ç¤Ë¡¢¥Í¥Ã¥È¥ï¡¼¥¯¤«¤é²èÁü¥Õ¥¡¥¤¥ë¤ò¼èÆÀ¤¹¤ë¥µ¥ó¥×¥ë¤Ç¤¹¡£
DefaultHttpClient client = new DefaultHttpClient(); httpResponse = client.execute(new HttpGet(url)); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { httpEntity = httpResponse.getEntity(); final InputStream in = httpEntity.getContent(); Bitmap bm = BitmapFactory.decodeStream(in); imageView.setImageBitmap(bm); }
¤³¤Î¥Ú¡¼¥¸¤Ø¤Î¥³¥á¥ó¥È
fbkqMq Really appreciate you sharing this article.Much thanks again. Awesome.
fHdqDy <a href="http://dksobeslhgqy.com/">dksobeslhgqy</a>, [url=http://copwcgwvvbyq.com/]copwcgwvvbyq[/url], [link=http://dbcxpwqxqzwh.com/]dbcxpwqxqzwh[/link], http://ucqiajbmyjez.com/