New in Android and
Working on an app like This
At the end got the problem java.io.FileNotFoundException: /document/86: open failed: ENOENT (No such file or directory)
Now I'm working on the solution that
"That how to use ContentResolver and openInputStream()"
Anyone help me how to do this, please
Asked
Active
Viewed 1.2k times
8
-
what kind of problems do you have with `InputStream`? – pskink Oct 10 '18 at 16:53
1 Answers
8
if you trying to make an app that read a file from Phone Try This
Try this may help
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();
Log.v("###", "yo " + PathHolder);
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 7:
if (resultCode == RESULT_OK) {
setContentView(R.layout.activity_main);
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();
}
}

Community
- 1
- 1

Krishna Murari
- 144
- 13
-
That input stream etc may need to be closed in the catch blocks. Maybe use try-with-resources. – Stan Nov 15 '21 at 19:06