android文件流-基础

 


---------------------写入手机内存-------------------------
    EditText et11=null;
    Button btn11=null;
    EditText et22=null;
    Button btn22=null;
    private final  String fileName =  "temp.txt";  
    private FileOutputStream fos = null;    
    private FileInputStream fis = null;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
       
        LinearLayout ll=    (LinearLayout) this.getLayoutInflater().inflate(R.layout.main1, null);
       
        et11= (EditText) ll.findViewById(R.id.et011);
        btn11= (Button) ll.findViewById(R.id.btn011);
        et22= (EditText) ll.findViewById(R.id.et022);
        btn22= (Button) ll.findViewById(R.id.btn022);
        btn11.setText("write");
        btn22.setText("read");
       
        btn11.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    try {
     fos = WriteOrReadActivity.this.openFileOutput(fileName,      Context.MODE_APPEND);
     
     String userInput = et11.getText().toString();     
     byte[] text = userInput.getBytes();
     fos.write(text);       
     
    } catch (Exception e) {
     e.printStackTrace();
     
    }finally{
     if (fos != null){
      try {
       fos.flush();
       fos.close();
      } catch (IOException e) {
       e.printStackTrace();
      }   
     }
    }
   }
  });
       
        btn22.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    try {
     
     fis = WriteOrReadActivity.this.openFileInput(fileName);
     
     //文件中没有数据
     if (fis.available() == 0) {
      return;
     }
     
     byte[] readBytes = new byte[fis.available()];
     while (fis.read(readBytes) != -1) {
      
     }
     
     String userData = new String(readBytes);     
     et22.setText(userData);       
     
     } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
     }finally{
      try {
       fis.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
   }
  });       
        setContentView(ll);


---------------------读取手机内存-------------------------

    EditText et=null;
    Button btn=null;
    private final static String fileName =  "temp_sms";
    private final static int fileMode = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
       
        LinearLayout ll=    (LinearLayout) this.getLayoutInflater().inflate(R.layout.main, null);
       
        et= (EditText) ll.findViewById(R.id.et01);
        btn= (Button) ll.findViewById(R.id.btn01);
        btn.setText("send");
       
        btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    SharedPreferences sp= fileActivity.this.getSharedPreferences (fileName, fileMode);
    String userInput = et.getText().toString();
         
    SharedPreferences.Editor editor = sp.edit();         
       
       editor.putString("username", userInput);         
       editor.commit();
   }
  });
       
        setContentView(ll);
    }


---------------------读取手机内存1-------------------------

 TextView tv=null;
 ImageView iv=null;
 Button btn=null;
 InputStream is=null;
 String imgPath="/data/data/com.android/afs.jpg";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
       
       
        tv=(TextView) this.findViewById(R.id.tv01);
        iv=(ImageView) this.findViewById(R.id.iv01);
        btn=(Button) this.findViewById(R.id.btn01);
       
  
        Drawable d= this.getResources().getDrawable(R.drawable.practice20_1);
        iv.setImageDrawable(d);       
       
        btn.setOnClickListener(new OnClickListener() {
                     
   @Override
   public void onClick(View v) {
    
       try {        
        is=new FileInputStream(imgPath);
        Bitmap b= BitmapFactory.decodeStream(is);
        iv.setImageBitmap(b);
        
        tv.setText(imgPath);
    } catch (IOException e) {
    
     e.printStackTrace();
    }finally{
     try {
      is.close();
     } catch (IOException e) {      
      e.printStackTrace();
     }
    }
   }
  });
       
    }   
   
    //把byte 转成  Bitmap
    private Bitmap getBitmap(byte[] b){
     if(b!=null){
      return BitmapFactory.decodeByteArray(b, 0, b.length);
     }else{
      return null;
     }
     
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值