private void writeDataFromInternet(String str) { try { File file = new File(Environment.getExternalStorageDirectory(), "cache.txt"); if (!file.exists()){ file.createNewFile(); } OutputStream fos = new FileOutputStream(file); fos.write(str.getBytes()); Log.i("=====", "writeDataFromInternet: "+str); fos.close(); } catch (IOException e) { e.printStackTrace(); } } //判断sdcard卡是否挂载 public boolean isSdCardExist() { Log.e("=====", "isSdCardExist: "); return Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); } //读取//读取sdcard文件 private void readFileFromSdcard(){ try { File file = new File(Environment.getExternalStorageDirectory(), "cache.txt"); FileInputStream is = new FileInputStream(file); byte[] b = new byte[is.available()]; is.read(b); String result = new String(b); System.out.println("读取成功:"+result); Gson gson=new Gson(); ComicBean comicBean = gson.fromJson(result, ComicBean.class); Log.e("=====", "readFileFromSdcard: "+comicBean.getData().getComics().get(0).getTitle()); GridLayoutManager manager = new GridLayoutManager(getContext(), 3); rv.setLayoutManager(manager); MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(getContext()); adapter.setData(comicBean.getData().getComics()); rv.setAdapter(adapter); adapter.notifyDataSetChanged(); } catch (Exception e) { e.printStackTrace(); } }
sdcard之写入和读取
最新推荐文章于 2021-05-25 21:04:34 发布