Android File 转Inputstram,跳转手机sdcard 获取指定文件

    xiaoxiao2021-03-25  108

    /** * 跳转sdcard * @param sel 是 onActivityResult中 requestCode的值,自定义 */ private void selcetFile(int sel) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(intent, sel); }

    //返回值处理 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null) return; String uri = data.getData().toString(); setText("文件路径:" + uri); }

    String --> InputStream ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());

    InputStream --> String String inputStream2String(InputStream is){    BufferedReader in = new BufferedReader(new InputStreamReader(is));    StringBuffer buffer = new StringBuffer();    String line = "";    while ((line = in.readLine()) != null){      buffer.append(line);    }    return buffer.toString(); }

    File --> InputStream

    InputStream in = new FileInputStream(file);

     

    InputStream --> File

    public void inputstreamtofile(InputStream ins,File file){    OutputStream os = new FileOutputStream(file);    int bytesRead = 0;    byte[] buffer = new byte[8192];    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {       os.write(buffer, 0, bytesRead);    }    os.close();    ins.close(); }

    转载请注明原文地址: https://ju.6miu.com/read-12484.html

    最新回复(0)