使用ContentResolver和openInputStream()(Android)

时间:2018-10-10 15:37:00

标签: java android android-contentresolver

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

1 个答案:

答案 0 :(得分:1)

如果您尝试制作一个可从手机读取文件的应用 尝试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();
    //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);
            }