I'm having a problem loading an image from the gallery in my Android app. I have some code that picks an image in emulator. When I make a selection, an exception is thrown and caught, meaning the image doesn't load, but I'm not sure how to diagnose what's happening.
Here is my java code file:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
galleryImage = (ImageView) findViewById(R.id.imageView);
pick_btn = (Button)findViewById(R.id.button);
pick_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,RESULT_LOAD_IMG);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
Log.i("Image Column Index..", "hoiiii");
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
galleryImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}catch (Exception e)
{
Toast.makeText(getApplicationContext(),"You haven't picked any image",Toast.LENGTH_LONG).show();
}
}
}