ContentResolver和openInputStream

ContentResolver和openInputStream

Android和
在像This这样的应用程序上工作
最后出现了问题java.io.FileNotFoundException: /document/86: open failed: ENOENT (No such file or directory)现在我正在研究解决方案
“该如何使用 ContentResolver和openInputStream()
有人帮我怎么做吗

最佳答案

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

试试这个可能有帮助

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

关于java - 使用ContentResolver和openInputStream()(Android),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52743934/

10-10 17:15