Hello Developers, In this blog we have given an amazing solution of file picker without any storage permission. You may know that Googe play has restricted MANAGE EXTERNAL STORAGE in Android 11 (SDK Version 30 ) or more device. Lets Start
File Picker Intent
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//set mime type
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
try {
startActivityIfNeeded(Intent.createChooser(intent, "Select a File"), 101);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
//if file manager not found on users device
}
On Activity Result
@Override
protected void onActivityResult(int requestCode, int _resultCode, @Nullable Intent _data) {
super.onActivityResult(requestCode, _resultCode, _data);
if (requestCode == 101) {
if (_resultCode == Activity.RESULT_OK) {
if (_data != null) {
ArrayList<Uri> fileUri = new ArrayList<>();
if (_data.getClipData() != null) {
for (int _index = 0; _index < _data.getClipData().getItemCount(); _index++) {
ClipData.Item _item = _data.getClipData().getItemAt(_index);
fileUri.add(_item.getUri());
}
} else {
fileUri.add(_data.getData());
}
// // here given an example of its uses
// for (int i = 0; i < fileUri.size(); i++) {
// // imageView.setImageURI(fileUri.get(i));
//
// }
//
} else {
Toast.makeText(getApplicationContext(), "Opening Failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "User not selected any file", Toast.LENGTH_SHORT).show();
}
}
}
Here I have created an example prject you can watch below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/pickFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick File"/>
</LinearLayout>
MainActivity.java
package com.sort.file_picker;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button pickFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pickFile = findViewById(R.id.pickFile);
pickFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//set mime type
intent.setType("/");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
try {
startActivityIfNeeded(Intent.createChooser(intent, "Select a File"), 101);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
//if file manager not found on users device
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int _resultCode, @Nullable Intent _data) {
super.onActivityResult(requestCode, _resultCode, _data);
if (requestCode == 101) {
if (_resultCode == Activity.RESULT_OK) {
if (_data != null) {
ArrayList<Uri> fileUri = new ArrayList<>();
if (_data.getClipData() != null) {
for (int _index = 0; _index < _data.getClipData().getItemCount(); _index++) {
ClipData.Item _item = _data.getClipData().getItemAt(_index);
fileUri.add(_item.getUri());
}
} else {
fileUri.add(_data.getData());
}
// // here given an example of its uses
// for (int i = 0; i < fileUri.size(); i++) {
// // imageView.setImageURI(fileUri.get(i));
//
// }
//
} else {
Toast.makeText(getApplicationContext(), "Opening Failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "User not selected any file", Toast.LENGTH_SHORT).show();
}
}
}
}
If you want to more projects related to Android 11 storage solution then please comment below, we are continiously trying to solve developers problem related to Android Studio java. Hope you get some help from our website. You can also read our popular posts, you may get some other help. I strongly reccomend to visit our youtube chennal. Thank You
0 Comments