图片与字符串的写入文件与读取

获取视图

downImg = (Button) findViewById(R.id.down_img);
        readImg = (Button) findViewById(R.id.read_img);
        downJson = (Button) findViewById(R.id.down_json);
        readJson = (Button) findViewById(R.id.read_json);
        img = (ImageView) findViewById(R.id.img);

设置监听 开启异步任务下载

downJson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyjsonAsyncTask(MainActivity.this).execute("https://gitee.com/little_bird_oh_777/test_data_collection/raw/master/message.json");

            }
        });
        readJson.setOnClickListener(new View.OnClickListener() {
            StringBuilder stringBuilder=new StringBuilder();
            @Override
            public void onClick(View v) {
                try {
                    FileInputStream fileInputStream = openFileInput("json.txt");
                    byte[] bys=new byte[1024];
                    int len=0;
                    while ((len=fileInputStream.read(bys))!=-1){
                        stringBuilder.append(new String(bys,0,len));
                    }
                    Log.i("---",stringBuilder.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        downImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyimgAsyncTask(MainActivity.this,img).execute("https://c-ssl.duitang.com/uploads/item/201711/05/20171105160320_ABZrK.jpeg");

            }
        });
        readImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

进行网络URL链接 从path下载字符串

 try {
            URL url=new URL(strings[0]);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(3000);
            urlConnection.setConnectTimeout(3000);
            urlConnection.connect();
            if(urlConnection.getResponseCode()==200){
                InputStream inputStream = urlConnection.getInputStream();
                byte[] bys=new byte[1024];
                int len=0;
                while((len=inputStream.read(bys))!=-1){
                    stringBuilder.append(new String(bys,0,len));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();

onPostExecute方法获取字符串

写入文件

try {
            FileOutputStream fileOutputStream = activity.openFileOutput("json.txt", Context.MODE_PRIVATE);
            fileOutputStream.write(s.getBytes());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }```
### 下载图片 写入文件

``
try {
            URL url=new URL(strings[0]);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();
            if(urlConnection.getResponseCode()==200){
                InputStream inputStream = urlConnection.getInputStream();
                byte[] bys=new byte[1024];
                int len=0;
                while((len=inputStream.read(bys))!=-1){
//                    stringBuilder.append(new String(bys,0,len));
                    by.write(bys,0,len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.i("---","=======");
            try {
                FileOutputStream fileOutputStream = activity.openFileOutput("img.png", Context.MODE_PRIVATE);
                fileOutputStream.write(by.toByteArray());
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

bitmap 填充视图

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File file = Environment.getExternalStorageDirectory();
            File file1=new File(file,"img.png");
            bitmap= BitmapFactory.decodeFile(file1.getAbsolutePath());
            img.setImageBitmap(bitmap);
        }```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值