简体   繁体   English

如何从Intent接收文件路径

[英]How to recieve file path from Intent

In my app I have a textedit that holds the path to the file my app is to process. 在我的应用程序中,我有一个textedit,其中包含我的应用程序要处理的文件的路径。 I have a browse button so the user can choose the file. 我有一个浏览按钮,因此用户可以选择文件。 After choosing the textedit should containbthe path/file information. 选择textedit后,应包含路径/文件信息。 In the button's onClick method I have the following code: 在按钮的onClick方法中,我有以下代码:

 Intent intent = new Intent( Intent.ACTION_GET_CONTENT ); 
 intent.setType ( "*/*" );
 startActivity ( Intent.createChooser ( intent, "Select a file" ) );

How do I get the Uri after the user is done? 用户完成后如何获得Uri?

startActivityForResult() per dev guide , recommended at the top of the Android docs on Intents 每个开发人员指南的 startActivityForResult()在Intents上的Android文档顶部建议使用

Starting an activity 开始活动

An Activity represents a single screen in an app. 活动代表应用程序中的单个屏幕。 You can start a new instance of an > Activity by passing an Intent to startActivity(). 您可以通过将Intent传递给startActivity()来启动> Activity的新实例。 The Intent describes the activity to start and carries any necessary data. 意图描述了要启动的活动并携带任何必要的数据。

If you want to receive a result from the activity when it finishes, call startActivityForResult(). 如果要在活动结束时从活动中接收结果,请调用startActivityForResult()。 Your activity receives the result as a separate Intent object in your activity's onActivityResult() callback. 您的活动在活动的onActivityResult()回调中将结果作为单独的Intent对象接收。 For more information, see the Activities guide. 有关更多信息,请参见活动指南。

My final code, works for my needs 我的最终代码可以满足我的需求

    public void btnPress_onClick ( View v )
  {
   Intent intent = new Intent ( Intent.ACTION_GET_CONTENT );
    switch ( v.getId ( ) )
      {
        case R.id.btnFilesystem:
          setContentView ( R.layout.filesystem );
          txtFileSource = findViewById ( R.id.txtboxSourcePath );
          txtFileDest = findViewById ( R.id.txtboxDestPath );
          break;
        case R.id.btnTextEdit:
          setContentView ( R.layout.textedit );
          textBox = findViewById ( R.id.textEditField );
          break;
        case R.id.btnHelp:
          setContentView ( R.layout.help );
          break;
        case R.id.btnSecurity:
          setContentView ( R.layout.security );
          spnrPivotTable = findViewById ( R.id.spnrSelectPivotTable );
          showKey = findViewById ( R.id.chkShowKey );
          passKey = findViewById ( R.id.txtPassKey );
          break;
        case R.id.btnHome:
          setContentView ( R.layout.main );
          break;
        case R.id.btnBrowseSource: 
          intent.setType ( "*/*" );
          startActivityForResult ( Intent.createChooser ( intent, "Select a file" ), BROWSE_SOURCE );

          break;
        case R.id.btnBrowseDest:
          intent.setType ( "*/*" );
          startActivityForResult ( Intent.createChooser ( intent, "Select a file" ), BROWSE_DEST );
          break;
        default:
          FancyToast.makeText ( MainActivity.this, "Function not yet coded.", Toast.LENGTH_SHORT, FancyToast.INFO, true ).show ( ); 
      }
  }

@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data )
  {
    switch ( requestCode )
      {
        case BROWSE_SOURCE:
          if ( resultCode == RESULT_OK )
            {
              txtFileSource.setText ( data.getData ( ).getPath ( ) );
            } else
            {
              FancyToast.makeText ( MainActivity.this, "An Error occured", FancyToast.ERROR, FancyToast.LENGTH_LONG, true ).show ( );
            }
          break;
        case BROWSE_DEST:
          if ( resultCode == RESULT_OK )
            {
              txtFileDest.setText ( data.getData ( ).getPath ( ) );
            } else
            {
              FancyToast.makeText ( MainActivity.this, "An Error occured", FancyToast.ERROR, FancyToast.LENGTH_LONG, true ).show ( );
            }
          break;
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM