Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将Uri从onActivityResult()返回到主意图_Android_Onactivityresult - Fatal编程技术网

Android 将Uri从onActivityResult()返回到主意图

Android 将Uri从onActivityResult()返回到主意图,android,onactivityresult,Android,Onactivityresult,我正在开发一个应用程序,其中一个组件是打开一个文件。现在,我编写了一个“performFileSearch”方法来打开文件搜索,如下所示: public void performFileSearch() { // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file // browser. Intent intent = new

我正在开发一个应用程序,其中一个组件是打开一个文件。现在,我编写了一个“performFileSearch”方法来打开文件搜索,如下所示:

public void performFileSearch() {
            // 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);

            // 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 "*/*".

            //Mime Types : https://developers.google.com/drive/web/mime-types
            intent.setType("*/*");
           // Intent resultData = intent;
           // READ_REQUEST_CODE = 1, defined as a global variable
            startActivityForResult(intent, READ_REQUEST_CODE);

        }
执行此方法后,将调用“onActivityResult”方法:

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

        // super.onActivityResult(requestCode, resultCode, resultData);

        if (requestCode == READ_REQUEST_CODE
                && resultCode == Activity.RESULT_OK) {

            Uri uri = null;
            if (resultData != null) {
                test = false;
                uri = resultData.getData();
                ur1 = uri;
                try {
                    // You can ignore the try part, I'm interested in just the
                    // URL
                    String msg = readTextFromUri(uri);
                    TextView textView = new TextView(this);
                    textView.setText(msg);
                    setContentView(textView);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Log.i(TAG, "Uri: " + uri.toString());

            }

        }
    }
我已将ur1声明为全局变量,以存储从onActivityResult方法检索到的uri值,但ur1值始终为null

我从类的onCreate方法调用了“performFileSearch()”,但在调试期间,我发现即使在“performFileSearch()”方法返回后,ur1值仍然为null

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.roster_recycle);
            rObject = (RecyclerView)findViewById(R.id.recycler_view);
            LinearLayoutManager llm = new LinearLayoutManager(this);
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            rObject.setLayoutManager(llm);

            MyCards c1 = new MyCards("Abhinav", 123, true);
            MyCards c2 = new MyCards("Abhishek", 13, false);
            performFileSearch();
            onResume();
            c1.setName("Abhinav");
            c2.setName("Baba");
            List<MyCards> cardList = new ArrayList<MyCards>();
            cardList.add(c1);
            cardList.add(c2);
            adapter = new MyAdapter(Roster.this,cardList);
            rObject.setAdapter(adapter);
          //  Log.e(Tag, ur1.toString());
        }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.Floster\U recycle);
rObject=(RecyclerView)findViewById(R.id.recycler\u视图);
LinearLayoutManager llm=新的LinearLayoutManager(本);
llm.设置方向(线性布局管理器.垂直);
rObject.setLayoutManager(llm);
MyCards c1=新的MyCards(“Abhinav”,123,真);
MyCards c2=新的MyCards(“阿披实”,13,假);
performFileSearch();
onResume();
c1.设定名称(“Abhinav”);
c2.设定名称(“Baba”);
List cardList=new ArrayList();
卡片列表。添加(c1);
卡片列表。添加(c2);
adapter=新的MyAdapter(花名册、卡片列表);
rObject.setAdapter(适配器);
//Log.e(Tag,ur1.toString());
}
然而,我编写了一个onResume()方法,并意识到在控件进入onResume方法之后,我得到了ur1值。代码首先遍历onCreate()方法中的所有步骤,然后才调用onResume()


我的问题是:有没有办法在onCreate方法本身中检索URL?

不要自己调用onResume(),因为操作系统需要调用它。如果你真的在简历上签了ur1,那你就得早点。在创建时使用ur1是没有意义的,因为onCreate在用户选择文件之前已经完成了很长时间。因此,我如何才能等到用户选择了文件?您什么也不做。当用户选择文件时,将调用onActivityResult。所以你应该对文件做你想做的。是的,我也在做同样的事情。我通过从onActivityResult内部调用一个方法来解析文件,但我还想将文件的URI存储在一个变量中。一种解决方案是从onResume()调用所有方法,因为首先执行整个onCreate方法,然后在调用onResume()时,只有URI获得非空值。所有这些对我来说都没有意义。首先,您希望等待用户选择一个文件。因此,您需要等待调用onActivity结果并处理该文件。您还需要存储它的uri。好啊但是为了什么?请先告诉我。你所说的关于onResume及其方法的一切-onResume的方法?-对我来说毫无意义。