sd卡操作

sd卡操作

 private void writer() {
       //得到对象
       //参数一 xml文件的名字 参数二 模式 MODE_PRIVATE 指定该SharedPreferences数据只能被本应用程序读写
       SharedPreferences mainActivity = getSharedPreferences("login", MODE_PRIVATE);
       //编辑对象昂
       SharedPreferences.Editor edit = mainActivity.edit();
       //写数据
       edit.putString("user","博客");
       edit.putBoolean("isGame",true);
       edit.putFloat("price",12);
       edit.putLong("id",123456);
       edit.putInt("age",13);
       //提交数据
       edit.commit();
   }
   

 private void read() {
        SharedPreferences login = getSharedPreferences("login", MODE_PRIVATE);
        String string = login.getString("user", "默认值");
        int age=login.getInt("age",0);
        boolean isMan=login.getBoolean("isGame",false);
        float price=login.getFloat("price",0.0f);
        long id=login.getLong("id",0l);
        Toast.makeText(this, string+":"+age+":"+isMan+":"+price+":"+id, Toast.LENGTH_SHORT).show();
    }

public class Main2Activity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private EditText username;
    private EditText password;
    private CheckBox cb;
    private Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //初始化控件
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        cb=(CheckBox)findViewById(R.id.cb_remember);
        login=(Button)findViewById(R.id.login);

        //第二次登录的时候读取
        sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);
        boolean isclick=sharedPreferences.getBoolean("isclick",false);
        if (isclick){
            //是否勾选
            String username1 = sharedPreferences.getString("user", "默认值");
            String password1 = sharedPreferences.getString("password", "默认密码");
            username.setText(username1);
            password.setText(password1);
            cb.setChecked(true);
        }
        //写数据
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username2 = username.getText().toString();
                String password2 = password.getText().toString();

                    //判断记住密码是否被选中
                    if(cb.isChecked()){//存
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isclick",true);
                        edit.putString("user",username2);
                        edit.putString("password",password2);
                        edit.commit();

                    }else{//清空
                        SharedPreferences.Editor edit = sharedPreferences.edit();
                        edit.putBoolean("isclick",false);
                        edit.commit();
                    }

            }
        });
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity"
    android:orientation="vertical"
    >
    <EditText
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:layout_alignParentLeft="true"
            android:id="@+id/cb_remember"
            android:text="记住密码"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:layout_alignParentRight="true"
            android:id="@+id/login"
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>


</LinearLayout>


public class SDActivity extends AppCompatActivity {
    private Button btn_sd;
    private  Button btn_read;
    private  Button btn_down;
    private  Button btn_up;
    private ImageView iv_show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sd);
        String externalStorageState = Environment.getExternalStorageState();
        Log.i("---", "onCreate: "+externalStorageState);
        File externalStorageDirectory = Environment.getExternalStorageDirectory();
        Log.i("---", "onCreate: "+externalStorageDirectory.toString());
        File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        Log.i("---", "onCreate: "+externalStoragePublicDirectory);



        btn_sd=findViewById(R.id.btn_sd);
        btn_read=findViewById(R.id.btn_read);
        btn_down=findViewById(R.id.btn_down);
        btn_up=findViewById(R.id.btn_up);
        iv_show=findViewById(R.id.iv_show);
        //下载
        btn_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                write_bitmap("http://www.005.tv/uploads/allimg/181228/55-1Q22Q4152I93.jpg");
            }
        });
        //显示读取
        btn_up.setOnClickListener(new View.OnClickListener() {
            Bitmap bitmap=null;
            @Override
            public void onClick(View v) {
                File file = Environment.getExternalStorageDirectory();
                try {
                    bitmap = BitmapFactory.decodeStream(new FileInputStream(new File(file, "jianmo.jpg")));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                iv_show.setImageBitmap(bitmap);
            }
        });


        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = Environment.getExternalStorageDirectory();
                File file1 = new File(file, "a.txt");
                try {
                    FileInputStream fileInputStream = new FileInputStream(file1);
                    int len=0;
                    byte[] bys=new byte[1024];
                    while((len=fileInputStream.read(bys))!=-1){
                        String s = new String(bys, 0, len);
                        Toast.makeText(SDActivity.this, "", Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        btn_sd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                //运行时权限
//                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},110);
                   //判断版本以及判断吗
                    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                        String[] str=new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
                        requestPermissions(str,100);
                    }
                File file = Environment.getExternalStorageDirectory();
                try {
                    FileOutputStream out = new FileOutputStream(new File(file, "a.txt"));
                    out.write("写入SD".getBytes());
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==110&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
            Log.i("---", "onRequestPermissionsResult: 同意");
        }else {
            Log.i("---", "onRequestPermissionsResult: 不同意");
//            finish();
        }
    }
    //读取图片
    public Bitmap read_bitmap(String filename){
        Bitmap bitmap=null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File file = Environment.getExternalStorageDirectory();
            File file1 = new File(file, filename);
            //通过图片路径生成bitmap对象
            bitmap=BitmapFactory.decodeFile(file1.getAbsolutePath());
        }

        return bitmap;
    }
    //下载图片到sd卡中
    public  void write_bitmap(String urlstring){
       new MyTask().execute(urlstring);

    }
    class  MyTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            FileOutputStream out=null;
            InputStream inputStream=null;
            HttpURLConnection connection=null;
            try {
                URL url= new URL(strings[0]);
                connection= (HttpURLConnection) url.openConnection();

                if (connection.getResponseCode()==200){
                    inputStream = connection.getInputStream();
                    //TODO 获取SD卡的路径
                    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//是否挂载
                        File file = Environment.getExternalStorageDirectory();
                        out = new FileOutputStream(new File(file,"jianmo.jpg"));
                        byte[] bytes=new byte[1024];
                        int len=0;
                        while((len=inputStream.read(bytes))!=-1){
                            out.write(bytes,0,len);
                        }
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值