您的位置:首页 > 移动开发 > Android开发

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

2017-03-09 15:35 393 查看
/**
* 跳转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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: