Reputation: 94
I'm trying to get an image by starting an Intent
to select from the photo gallery using startActivityForResult
and by overriding onActivityResult
. I don't know why, but when I select an image and try to draw it on my screen, nothing happens. I'm able to select the image, and then that's it.
I searched for answers but couldn't find anything that helped. There doesn't seem to be much that I can try to fix this problem. I believe I've implemented everything correctly. This is also my first time trying this though so I could be wrong. I've overrided the onActivityResult
to get a bitmap and set a bitmap variable from another class with it when I call getGalleryImage()
.
Starting the Intent from my GameView class:
MainActivity activity = (MainActivity) context;
public void getGalleryImage() {
Intent gallery = new Intent(Intent.ACTION_PICK);
gallery.setType("image/*");
String[] mimeTypes = { "image/png" };
gallery.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
activity.startActivityForResult(gallery, 1);
}
Override Method in MainActivity:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == MainActivity.RESULT_OK) {
final Uri uri = data.getData();
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
final Bitmap selected_img = BitmapFactory.decodeStream(in);
view.setSelectedImage(selected_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "You didn't pick an image!", Toast.LENGTH_LONG).show();
}
}
}
I want to get the image and draw it to my screen. What's happening is I select the image after the intent opens, and then nothing. It goes back to my activity and nothing seems to be drawing at all.
Upvotes: 2
Views: 5767
Reputation: 1037
As it seems like your View.setSelectedImage()
comes from a custom class, you might want to try trying to draw the Bitmap
on ImageView
first to see if it works.
Use ImageView
instead and call ImageView.setImageBitmap(bitmap)
from within onActivityResult()
. And make sure decoded bitmap isn't too large as it can cause your app to stutter and you might want to try loading large bitmap efficiently.
Also, update your getGalleryImage()
method to this:
public void getGalleryImage() {
final Intent galleryIntent = Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 1);
}
Upvotes: 1
Reputation: 544
you have to provide a provider from android version 7 (nougat)
First Set provider in manifest
</application>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
Next define a xml file path named filepaths inside xml folder in resource :
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
Next call intent to pick image :
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
String[] mimeTypes = {"image/jpeg", "image/png"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, GALLERY_REQUEST_CODE);
in your onactivity result :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImage = null;
if (requestCode == GALLERY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
selectedImage = data.getData();
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
final Bitmap selected_img = BitmapFactory.decodeStream(in);
yourimageview.setImageBitmap(selected_img);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "An error occured!", Toast.LENGTH_LONG).show();
}
}
}
}
Upvotes: 3