SD卡得读取 案例

布局

<Button
        android:id="@+id/writejson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入json串"/>

    <Button
        android:id="@+id/readjson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取json串"/>

    <Button
        android:id="@+id/readpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="从SD卡中读取一张图片"/>

    <Button
        android:id="@+id/urlpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="从网络下载一张图片"/>

    <TextView
        android:id="@+id/showjson"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="显示数据"
        android:textColor="#000"
        android:layout_marginTop="20dp"/>

    <ImageView
        android:id="@+id/showpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_marginTop="10dp"/>

网络请求

 @Override
    protected String doInBackground(String... strings) {
        FileOutputStream fos = null;
        InputStream is = 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){
                is = connection.getInputStream();
                //判断SD卡是否挂载
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        //获取SD卡的路径
                    File externalStorageDirectory = Environment.getExternalStorageDirectory();
                    fos = new FileOutputStream(new File(externalStorageDirectory,"wangbolup.jpg"));
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = is.read(bytes))!=-1){
                        fos.write(bytes,0,len);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {//关流
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {

                }
            }
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                connection.disconnect();
            }
        }
        return null;
    }

网络下载数据,图片,和SD卡读取数据,图片

//声明四个按钮
    private Button writejson;
    private Button readjson;
    private Button urlpic;
    private Button readpic;
    private TextView showjson;
    private ImageView showpic;
//    private String json = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
    private String json = "http://www.181010.com/mmw/uploads/allimg/130702/2-130F2201934514.jpg";
    private String picurl="http://www.181010.com/mmw/uploads/allimg/130702/2-130F2201934514.jpg";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //判断SD卡是否挂载
        final String externalStorageState = Environment.getExternalStorageState();
        //获取SD看的根目录
        final File externalStorageDirectory = Environment.getExternalStorageDirectory();
        //获取SD卡公开目录pictures文件
        File externalStoragePublicDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

        //初始化
        writejson = (Button) findViewById(R.id.writejson);
        readjson = (Button) findViewById(R.id.readjson);
        urlpic = (Button) findViewById(R.id.urlpic);
        readpic = (Button) findViewById(R.id.readpic);
        showjson = (TextView) findViewById(R.id.showjson);
        showpic = (ImageView) findViewById(R.id.showpic);


        //设置动态请求SD卡的权限
        requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);



        //写入json的点击事件
        writejson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断SD卡是否挂载
                if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
                    //获取sd卡的根路径
//                    File externalStorageDirectory = Environment.getExternalStorageDirectory();
                    FileOutputStream fos = null;

                    try {
                        //File(要写到的位置.写成的文件名字和格式);
                        fos = new FileOutputStream(new File(externalStorageDirectory,"json.txt"));
                        fos.write(json.getBytes());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if (fos!=null){
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                    Log.i("tag", "onClick: 写入成功");
                }
            }
        });

        //读取json的点击事件
        readjson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取SD卡的根路径
//                File externalStorageDirectory1 = Environment.getExternalStorageDirectory();
                //查看SD卡是否挂载
//                String externalStorageState1 = Environment.getExternalStorageState();


                //判断SD卡是否挂载了
                if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
                    FileInputStream is = null;
                    StringBuffer sb = new StringBuffer();
                    try {
                        is = new FileInputStream(new File(externalStorageDirectory,"json.txt"));
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        while ((len = is.read(bytes))!=-1){
                            sb.append(new String(bytes,0,len));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        if (is != null){
                            try {
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    Log.i("tag", "onClick: 读取成功");
                    showjson.setText(sb.toString());
                }

            }
        });


        //从网上下载一张图片到SD卡中
        urlpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进行网络下载,要用到异步
                new MyTask().execute(picurl);
            }
        });


        //从SD卡中读取图片
        readpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = null;
                //判断SD卡是否挂载
                if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
                    File file = new File(externalStorageDirectory, "wangbolup.jpg");
                    //BitmapFactory可以直接根据SD卡图片路径转成一个bitmap对象
                    bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                }
                showpic.setImageBitmap(bitmap);
            }
        });


    }

    //设置SD卡授权的提示   授权成功和授权失败时候分别作出的回应
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 &&grantResults[0] == PackageManager.PERMISSION_GRANTED){
        }else{
            finish();
            Toast.makeText(this, "未获授权", Toast.LENGTH_SHORT).show();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值