Hello developers, if you are facing problem while pick file from Android 11 using Intent in Android Studio then you are in the right place. Here I have solved the problem and also given example source code, it can help you to pick file on Latest Android Versions. This code will work in All Android Versions. I have tasted this code on Android 10, 11, 12. If you face any problem with this code in any specific Android version, then contact with details. I will try to help you as soon as possible.
Android 11 image picker not working Android Studio
private void pickImage() {
try {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// you can add a specific image like png, jpg..etc.
// intent.setType("image/png");
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityIfNeeded(Intent.createChooser(intent, "Select an Image"), 101);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@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) {
onImagePicked(data);
} else {
Toast.makeText(getApplicationContext(), "Opening Failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "File not picked", Toast.LENGTH_SHORT).show();
}
}
}
private void onImagePicked(Intent data) {
//here is the uri of the image
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
// I am giving some example for using this uri
// InputStream inputStream = getContentResolver().openInputStream(imageUri);
}
Image Picker Source Code
Here I have given example source code of image picker. You can also pick other files by just changing the myme tipe in PickFile() void. Create a project in Android Studio and also create an Activity. After created just paste this activity code and layout xml code.
package com.images.picker;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
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.Toast;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
Button pickButton;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pickButton = findViewById(R.id.pickButton);
imageView = findViewById(R.id.imageView);
pickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickImage();
}
});
}
private void pickImage() {
try {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// you can add a specific image like png, jpg..etc.
// intent.setType("image/png");
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false);
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityIfNeeded(Intent.createChooser(intent, "Select an Image"), 101);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Please install a File Manager", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@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) {
onImagePicked(data);
} else {
Toast.makeText(getApplicationContext(), "Opening Failed", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "File not picked", Toast.LENGTH_SHORT).show();
}
}
}
private void onImagePicked(Intent data) {
//here is the uri of the image
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
// I am giving some example for using this uri
// InputStream inputStream = getContentResolver().openInputStream(imageUri);
}
}
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"/>
<Button
android:id="@+id/pickButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="PICK File"/>
</LinearLayout>
Hope this post helps you and if you face any problem related to Android Studio then contect me. Thank You
0 Comments