I have been trying to select an image from the gallery and then set an imageview on Android by using the following code, but when I select an image, it goes back to the "select an image screen" (i.e. the first screen that comes up after the intent starts) and after about 3 seconds, the screen turns black. What's odd is that no errors are called, and about 1 in every 10 attempts, it actually works. I am running 5.0.1 Lollipop on a Nexus 6, and I have figured out that onActivityResult is not being called whenever the screen goes black. Thanks for any help,
Jacob
Here is my code:
public static int GAN = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_funky);
Button a = (Button) findViewById(R.id.button);
ImageView b = (ImageView) findViewById(R.id.imageView5);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GAN);
System.out.println("hello world");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("hello world");
if (requestCode == GAN && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView5);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}