sd卡存入或读取图片

sd卡的读取

获取权限

 /**
     * 动态权限申请
     */
    private void chenkAccess(){
        //判断是否是android6.0
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            //TODO 如果是android6.0,则继续申请权限
            //判断当前你想要的申请的权限是否已经授权了。
            int access =  ContextCompat.checkSelfPermission(getApplication(),Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(access== PackageManager.PERMISSION_GRANTED){//曾经授权过了,值=0
                //TODO  该干嘛干嘛,继续你的工作
                doSomeThing();
            }else{//没有授权过
                //TODO  去申请授权
                //弹出一个系统的对话框
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},WRITE_EXTERNAL_STORAGE_GET);
            }
        }else{
            //TODO  该干嘛干嘛,继续你的工作
            doSomeThing();
        }
    }

    /**
     * 动态权限回调
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode==WRITE_EXTERNAL_STORAGE_GET){
            //判断是否是请求的权限,如果是,再判断这个权限用户是否授权
            if(permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    &&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                //TODO  该干嘛干嘛,继续你的工作
                doSomeThing();
            }else{
                //TODO 根据需要去处理
                Toast.makeText(this,"您不能继续使用此功能",Toast.LENGTH_LONG).show();
                //不同意我不干了
                finish();
            }
        }
    }
    private void doSomeThing(){
        Toast.makeText(this,"我真的使用这个权限作了很重要的工作",Toast.LENGTH_LONG).show();
    }

json

 //方法1:向SD卡中写json串
    public static void write_json(String json)  {
        //判断是否挂载
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //获取SD卡根路径:mnt/shell/emulated/0
            File file=Environment.getExternalStorageDirectory();
            FileOutputStream out=null;
            try {
                //创建输出流
                out= new FileOutputStream(new File(file,"json.txt"));
                out.write(json.getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(out!=null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
    }
    //方法2:从SD卡中读取json串
    public static String read_json() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File file = Environment.getExternalStorageDirectory();
            FileInputStream inputStream = null;
            StringBuffer sb=new StringBuffer();
            try {
                inputStream=new FileInputStream(new File(file,"json.txt"));
                byte[] b=new byte[1024];
                int len=0;
                while((len=inputStream.read(b))!=-1){
                    sb.append(new String(b,0,len));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return  sb.toString();
        }else{
            return "";
        }
    }

图片

//方法3:从SD卡中读取一张图片
    public  static  Bitmap read_bitmap(String filename) {//filename图片名字
        Bitmap bitmap=null;
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File file=Environment.getExternalStorageDirectory();
            File file1 = new File(file, filename);
            //BitmapFactory可以直接根据SD卡图片路径转成一个bitmap对象
             bitmap= BitmapFactory.decodeFile(file1.getAbsolutePath());
        }
        return bitmap;
    }
    //方法4:网络下载一张图片存储到SD卡中
    public  static  void write_bitmap(String url) {//网址
       new MyTask().execute(url);
    }
    static class MyTask extends AsyncTask<String,String,String> {
        @Override
        protected String doInBackground(String... strings) {
            FileOutputStream out=null;
            InputStream inputStream=null;//网络连接的输入流
            HttpURLConnection connection=null;//向SD卡写的输出流
            try {
                URL url= new URL(strings[0]);
                connection= (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(5*1000);
                connection.setReadTimeout(5*1000);
                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,"xiaoyueyue.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();
            }finally {
                //关流
                if(out!=null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(inputStream!=null){
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(connection!=null){
                    connection.disconnect();
                }
            }
            return null;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值