using ContentResolver and openInputStream() (Android)

Krishna Bagwari :

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

krishna :

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();
                }
            }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=71202&siteId=1