使用 ContentResolver 和 openInputStream() (Android)

using ContentResolver and openInputStream() (Android)

Android 中的新内容和 在 这样的应用上工作 最后遇到问题java.io.FileNotFoundException: /document/86: open failed: ENOENT (No such file or directory) 现在我正在研究解决方案 "That how to use ContentResolver and openInputStream()" 任何人都可以帮助我如何做到这一点,请

如果您尝试制作一个从 Phone 读取文件的应用 尝试

试试这个可能会有帮助

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