![](/img/trans.png)
[英]ContentResolver openInputStream downloads the file in the background
[英]using ContentResolver and openInputStream() (Android)
在新的Android和喜歡的應用工作這在結束了這個問題java.io.FileNotFoundException: /document/86: open failed: ENOENT (No such file or directory)
現在我工作的解決方案,“那怎么使用ContentResolver和openInputStream() “請幫助我如何做到這一點
如果你試圖制作一個從手機上讀取文件的應用程序試試這個
試試這可能有所幫助
private InputStream getResources(String s) {
return null;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent, 7);
Log.v("###", "parent " + getParent());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri PathHolder = data.getData();
//String PathHolder = data.getDataString();
Log.v("###", "yo " + PathHolder);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 7:
if (resultCode == RESULT_OK) {
setContentView(R.layout.activity_main);
//PathHolder = data.getData();
// FileInputStream fileInputStream = null;
// StringBuilder text = new StringBuilder();
try {
InputStream inputStream = getContentResolver().openInputStream(PathHolder);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
String mLine;
while ((mLine = r.readLine()) != null) {
text.append(mLine);
text.append('\n');
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Log.v("###", "yo " + text);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.