如何从Intent接收文件路径

时间:2019-01-23 04:37:57

标签: java android android-intent

在我的应用程序中,我有一个textedit,其中包含我的应用程序要处理的文件的路径。我有一个浏览按钮,因此用户可以选择文件。选择textedit后,应包含路径/文件信息。 在按钮的onClick方法中,我有以下代码:

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

用户完成后如何获取Uri?

2 个答案:

答案 0 :(得分:1)

startActivityForResult()dev guide,建议在Android docs on Intents顶部

  

开始活动

     

活动代表应用程序中的单个屏幕。您可以通过将Intent传递给startActivity()来启动> Activity的新实例。该意图描述了启动活动并携带任何必要的数据。

     

如果要在活动结束时从活动中接收结果,请调用startActivityForResult()。您的活动在活动的onActivityResult()回调中将结果作为单独的Intent对象接收。有关更多信息,请参阅“活动”指南。

答案 1 :(得分:1)

我的最终代码可以满足我的需求

    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;
      }
  }