ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SharedPreferences存储数据及文件数据存储

2020-06-24 10:08:02  阅读:255  来源: 互联网

标签:存储 outputStream SharedPreferences Environment printStackTrace File catch new 数据


SharedPreferences存储数据及文件数据存储

一.什么是内部、外部存储

在这里插入图片描述

二.权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

6.0之后的动态权限

private String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET};
/**
         * 6.0以上加动态权限
         */
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            ActivityCompat.requestPermissions(this,permissions,100);
        }            

三.内部存储

在这里插入图片描述

获得路径方法

context.getCacheDir()
context.getFilesDir()

SharedPreferences存储

public void writeShare(){
        SharedPreferences sharedPreferences = getSharedPreferences("1804A",MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("istrue",false);
        editor.putFloat("float",0);
        editor.putInt("int",1);
        editor.putLong("long",1);
        editor.putString("string",null);
        Set<String> stringSet = new HashSet<String>();
        stringSet.add("张三");
        stringSet.add("李四");
        editor.putStringSet("set",stringSet);
        editor.commit();
    }
public void readShare(){
        SharedPreferences sharedPreferences = getSharedPreferences("1804A",MODE_PRIVATE);
        boolean result = sharedPreferences.getBoolean("istrue",true);
        //其他方法一样都使用getxxx来获得
    }

SharedPreferences读写方式

  • MODE_PRIVATE:本程序可以访问
  • MODE_WORLD_READABLE:允许其他应用程序读取文件(可读)
  • MODE_WORLD_WRITEABLE:允许其他应用程序修改文件(可写)

四.外部存储

在这里插入图片描述

获得公共目录方法

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)

文件读写

 /**
     * 写文件
     */
    public void writeFile(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            String path = Environment.getExternalStorageDirectory().getPath();
            String abPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(path+"/test.txt");
            FileOutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(file);
                outputStream.write("你好".getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(outputStream != null)
                        outputStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
/**
     * 读文件
     */
    public void readFile(){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            File file = Environment.getExternalStorageDirectory();
            File newFile = new File(file,"test.txt");
            StringBuffer stringBuffer = new StringBuffer();
            try {
                FileInputStream inputStream = new FileInputStream(newFile);
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(bytes)) != -1)
                {
                    stringBuffer.append(new String(bytes,0,len));
                }
                Log.d("amy",stringBuffer.toString());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
/**
     * 下载图片保存
     */
    public void saveImage(){
        InputStream inputStream = null;
        FileOutputStream outputStream = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(StrintImagePath);
            connection = (HttpURLConnection) url.openConnection();
            //连接成功
            if(connection.getResponseCode() == 200)
            {
                inputStream  = connection.getInputStream();
                byte[] bytes = new byte[1024];
                int len = 0;

                if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                    File file = Environment.getExternalStorageDirectory();
                    //D/amy: /storage/emulated/0
                    File newFile = new File(file,"image.png");
                    outputStream = new FileOutputStream(newFile);
                    while ((len = inputStream.read(bytes)) != -1){
                        outputStream.write(bytes,0,len);
                    }
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(inputStream != null)
                    inputStream.close();
                if(outputStream != null)
                    outputStream.close();
                if(connection!= null)
                    connection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 /**
     * 读图片
     */
    public void readImage(){
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = Environment.getExternalStorageDirectory();
            File newFile = new File(file,"image.png");
            try {
                FileInputStream inputStream = new FileInputStream(newFile);
                Bitmap bitmap = BitmapFactory.decodeFile(newFile.getAbsolutePath());
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

标签:存储,outputStream,SharedPreferences,Environment,printStackTrace,File,catch,new,数据
来源: https://blog.csdn.net/amynn/article/details/106887796

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有