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

来源:互联网 发布:al是什么软件 编辑:程序博客网 时间:2024/06/07 05:23

/** * 跳转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);}

//返回值处理@Overrideprotected 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();
}

0 0