Android存储(文件流和io流)

1.内部存储

内部存储用openFileOutput()和openFileInput(),它保存在/data/data/应用软件包名/files/下。

保存数据

private void save(){
        //要保存的图片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.fuyao);
        //把Bitmap转换成byte[]
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bitmapbyte = baos.toByteArray();
        //保存数据
        FileOutputStream out = null;
        try {
             out=openFileOutput("fu.png",MODE_PRIVATE);                       //打开要保存的文件路径
            out.write(bitmapbyte);                                            //写入数据
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


/    public void  save() {//文件输出流,用于保存信息
//
//        String pricesum2 = Integer.toString(pricesum);
//        String image2 = Integer.toString(image[position]);
//
//        String filename=app.account+".txt";//文件名称
//        String content =image2+"#"+text[position]+"#"+pricesum2+"#";  //保存数据
//        FileOutputStream fos=null;
//        try{
//
//            // 写入方式,支持两种方式:
//            //     *                   Context.MODE_PRIVATE;私有覆盖型
//            //     *                   Context.MODE_APPEND;私有追加型
//            //     */
//            // fos=openFileOutput(filename,MODE_PRIVATE);
//            fos=openFileOutput(filename, MODE_APPEND);
//
//            fos.write(content.getBytes());//将数据写入文件
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            try{
//                if(fos!=null) {
//                    fos.close();
//                }
//            }catch (Exception e) {
//                    e.printStackTrace();
//            }
//        }
//
//
//
//    }

读取数据

private void read(){

        FileInputStream in=null;
        try {
            //读取图片
            in=openFileInput("fu.png");
            byte[] b=new byte[in.available()];                        
            in.read(b);
            //把图片放进ImageView
            Bitmap bitmap=BitmapFactory.decodeByteArray(b,0,b.length);
            ImageView iv=findViewById(R.id.imageView2);
            iv.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


/    public void look(){//输入流
//        app = (AppData) RecordActivity.this.getApplication();              //获得全局类
//        String filename=app.account+".txt";//文件名称
//        String content="";
//        FileInputStream fis=null;
//        try{
//            fis=openFileInput(filename);
//            byte[] buffer=new byte[fis.available()];
//            fis.read(buffer);
//            content=new String(buffer);
//        } catch (Exception e) {
//
//            e.printStackTrace();
//        }finally {
//            try {
//                if (fis != null) {
//                    fis.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
//    }

2.外部存储

外部存储用FileOutputStream和FileInputStream类,理论上可以存储在手机里的任意位置,比如下面的位置:

 

Environment.getStorageDirectory()                      位置:/storage

 Environment.getDataDirectory() 位置:/data

 Environment.getRootDirectory() 位置:/system

 Environment.getExternalStorageDirectory() 位置:/storage/emulated/0

 getExternalCacheDir() 位置:/storage/emulated/0/Android/data/应用软件包名/cache

 getExternalFilesDir(null) 位置:/storage/emulated/0/Android/data/应用软件包名/files

 

 

保存数据:

private void save(){
        //要保存的图片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.xx);
        //把Bitmap转换为byte[]
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bitmapbyte = baos.toByteArray();
        //保存数据
        FileOutputStream out=null;
        try {
                                                                    //保存到/storage/emulated/0/DCIM/Camera目录
            File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera","vv.png");
            out=new FileOutputStream(file);
            out.write(bitmapbyte);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

读取数据:

private void read(){
        FileInputStream in=null;
        try {
            //读取图片
            File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera","vv.png");
             in=new FileInputStream(file);
             byte[] b=new byte[in.available()];
             in.read(b);
             //显示到ImageView
            Bitmap bitmap=BitmapFactory.decodeByteArray(b,0,b.length);
            ImageView iv=findViewById(R.id.imageView2);
            iv.setImageBitmap(bitmap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值