0

In my app I have a textedit that holds the path to the file my app is to process. I have a browse button so the user can choose the file. After choosing the textedit should containbthe path/file information. In the button's onClick method I have the following code:

 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?

2

2 Answers 2

1

startActivityForResult() per dev guide, recommended at the top of the Android docs on Intents

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(). 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(). Your activity receives the result as a separate Intent object in your activity's onActivityResult() callback. For more information, see the Activities guide.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok! That (and Swati's link) set me on what I hope is the right path. I am getting data, now to use it.
Thanks! It seemed you just needed a little push to get unstuck. Please/feel free to edit my answer with whatever you find for that onActivityResult() callback... if you can stand to!
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.