I have an activity that has a button. When I click on the button, it redirects me to the image gallery. I want to show the selected image in the next step using the image view. But it does not display the image. When the image is set, the image is turned off.
Below is my code for selecting an image and moving on to the next. I do not use any story in my actions.
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && 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(); if (!(picturePath.equals(""))) { Intent intent = new Intent(); intent.setClass(MainActivity.this, ImageInGellary.class); intent.putExtra("picturePath", picturePath); startActivity(intent); } } } public class ImageInGellary extends Activity { Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.load_image); cancel = (Button) findViewById(R.id.buttonCancelPicture); Intent in = getIntent(); savedInstanceState = in.getExtras(); String picturePath = savedInstanceState.getString("picturePath"); ImageView imageView = (ImageView) findViewById(R.id.img_view); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(); intent.setClass(ImageInGellary.this, MainActivity.class); startActivity(intent); } }); } }
source share