用Intent打开文件后Uri路径错误

时间:2019-05-10 00:52:46

标签: android file android-intent path uri

在我的应用程序中打开文件时遇到麻烦。我执行了一个Intent操作,以便可以通过系统文件打开文件。但是我无法收到我打开的文件的正确路径。

我正在使用具有Android 5.1的坚固平板电脑

public void openFile(View view) {
        // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
        // browser.
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

        // Filter to only show results that can be "opened", such as a
        // file (as opposed to a list of contacts or timezones)
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        // Filter to show only images, using the image MIME data type.
        // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
        // To search for all documents available via installed storage providers,
        // it would be "*/*".
        intent.setType("*/*");

        startActivityForResult(intent, READ_REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 Intent resultData) {

        // The ACTION_OPEN_DOCUMENT intent was sent with the request code
        // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
        // response to some other intent, and the code below shouldn't run at all.

        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            // The document selected by the user won't be returned in the intent.
            // Instead, a URI to that document will be contained in the return intent
            // provided to this method as a parameter.
            // Pull that URI using resultData.getData().
            Uri uri = null;
            if (resultData != null) {
                uri = resultData.getData();

                //showImage(uri);

                try {
                    InputStream kmlmap = new FileInputStream(uri.getPath());
                    if( kmlmap != null) {
                        Toast.makeText(this, "Mapa abierto", Toast.LENGTH_SHORT).show();
                        kmlLayer = new KmlLayer(mMap, kmlmap, getApplicationContext());
                        kmlLayer.addLayerToMap();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }
    }
}

在onActivityResult中,我调用getData(),而uri是“ content://com.android.providers.downloads.documents/document/80”,它应该是“ storage / emulated / 0 / Download / Dorrego.kml” < / p>

0 个答案:

没有答案