如何从Android中的URI / Uri打开文件

时间:2017-06-07 18:28:39

标签: java android

我正在尝试从我的模拟器的内部存储中打开一个文件(试图将它放在SD卡中,但它放在内部存储中?)。我让用户选择一个文件,然后通过意图将该文件的Uri(注意案例)传递给文本编辑活动。在这项活动中,我尝试了一些方法。我现在的代码看起来像这样(请原谅我的疯狂,我一直在尝试很多事情):

try {
        Intent intent = getIntent();
        File file;
        Uri uri = Parcels.unwrap(intent.getParcelableExtra("uri"));

        URI newUri = new URI("file://"+uri.toString());
        Log.d("uri", uri.toString());
        Log.d("newUri", newUri.toString());
        file = new File(newUri);
        if (intent != null) {

            Log.d("uriPath", uri.toString());
            if (uri != null) {

                file = new File(uri.getPath());
                String text = readTextFile(file);
                if (text != null) {
                    Log.d("in if text not null", text);
                    mEditText.setText(text);
                }
//        mEditText.setText(readTextFile(file));
            }
        }
    }
    catch(URISyntaxException e) { e.printStackTrace(); }

我尝试了很多东西,没有成功。我要么找不到文件(因为它没有为文件添加任何内容,只是尝试加载“document / primary%3Aindex.html”)或其他例外,目前:

06-06 05:43:18.826 10794-10794/net.a40two.pext E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: net.a40two.pext, PID: 10794
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{net.a40two.pext/net.a40two.pext.ui.EditorActivity}: java.lang.IllegalArgumentException: Found authority in URI: file://content://com.android.externalstorage.documents/document/primary%3Aindex.html
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                              Caused by: java.lang.IllegalArgumentException: Found authority in URI: file://content://com.android.externalstorage.documents/document/primary%3Aindex.html
                                                                 at java.io.File.checkURI(File.java:230)
                                                                 at java.io.File.<init>(File.java:175)
                                                                 at net.a40two.pext.ui.EditorActivity.onCreate(EditorActivity.java:67)
                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

即使没有file:// concatenation,也会发生这种情况。我在网上找到的大多数东西要么难以置信,要么不起作用,要么两者兼而有之。真诚地感谢任何帮助。

我在主要活动中获取此URI的代码直接来自developers.android.com:

    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();
            Log.i("onActivityResult", "Uri: " + uri.toString());

            Intent intent = new Intent(MainActivity.this, EditorActivity.class);
            intent.putExtra("uri", Parcels.wrap(uri));
            startActivity(intent);
            finish();

        }
    }
}

0 个答案:

没有答案