0% found this document useful (0 votes)
61 views

Android Pick File Programatically

This document describes how to programmatically pick a file in Android. It includes XML layout code for a button and Java code to launch a file picker intent when the button is clicked. Upon returning from the file picker, the path of the selected file is retrieved from the intent data and displayed in a Toast notification.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Android Pick File Programatically

This document describes how to programmatically pick a file in Android. It includes XML layout code for a button and Java code to launch a file picker intent when the button is clicked. Upon returning from the file picker, the path of the selected file is retrieved from the intent data and displayed in a Toast notification.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Android Pick File Programatically

References:
https://www.mytrendin.com/android-pick-file-programatically/

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<Button
android:text="File Picker "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_picker" />
</RelativeLayout>
package com.mytrendin.pickanyfile;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.btn_picker);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 7);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
// TODO Auto-generated method stub

switch (requestCode) {
case 7:
if (resultCode == RESULT_OK) {
String PathHolder = data.getData().getPath();
Toast.makeText(MainActivity.this, PathHolder,
Toast.LENGTH_LONG).show();
}
break;
}
}
}

You might also like