We will see how to implement this method step by step. For this we will first create a project.
Step 1. Create Project
First of all we will open this android studio and then create a project in it with any name which you want like it.
Step 2.
After creating the project we will see that we will see two files activity_main.xml and MainActivity.java To pick up the file, we need a Button and a TextView on which we will show the file path.Whose code is given below in which we have used a Button and TextView.
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Hello world"
android:textColor="@color/black"
android:layout_marginTop="170dp"/>
<Button
android:id="@+id/getUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Pick Up"/>
</RelativeLayout>
Now we will implement this layout in MainActivity.javaMainActivity.java
package com.avdigits.getfolder;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.textshow);
btn=findViewById(R.id.getUp);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,10);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 10:
if (resultCode==RESULT_OK)
{
String str=data.getData().getPath();
textView.setText(str);
}
}
}
}
In this code we have used onclickListener on button inside which we have used action, type and startActivityForResult using intent.We have given intent and a request code 10 as the andar parameters of StartActivityForResult. You can give the request code anything but it must be an integer value.Step 3.
And last we have to override a method onActivityResult in which we have used a switch case in which we have given request code (10).For switch case and we have used if statement in which the given parameter will be true if the code inside the if will run in which the path of the file will be printed to the text.
I hope this article will be very useful for you.
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji