Note that there are some explanatory texts on larger screens.

plurals
  1. POActivity Restarts onActivityResult(), after image selected from the SDCard
    text
    copied!<p>I have an application where i go to select an image from the gallery, and return with the image to set it on the imageview. The problem is, when i set the 1st image, and again go to select the next image using the same button, the 1st image is gone, and current image is set as the 1st image. I have a linearlayout and i dynamically create the imageviews as the images are selected. </p> <p>Below is my class, where i select the image on the button click btnSelectPhotosGallery, and then onActivityResult is there.</p> <pre><code>@Override public void onClick(View v) { int id = v.getId(); Intent i; switch (id) { //image selection on this button click case R.id.btnSelectPhotosGallery: Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_PICK); startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); break; default: break; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri selectedImageUri = data.getData(); String imagePath = CommonMethods.getPath(ArticleForm2.this, selectedImageUri); File image = new File(imagePath); long sizeOfImage = ((image.length()/1024)/1024); if(sizeOfImage &gt; 1.0) { Toast.makeText(ArticleForm2.this, "Image exceeding the Maximum Size(1 MB)", Toast.LENGTH_SHORT).show(); //coverPhotoSelected = false; } else { //imageCoverPhoto.setImageURI(selectedImageUri); Bitmap bmp = getPreview(imagePath); ImageView img = new ImageView(ArticleForm2.this); img.setLayoutParams(new LayoutParams(300, 300)); img.setImageBitmap(bmp); linearImages.addView(img); } } } } public Bitmap getPreview(String fileName) { File image = new File(fileName); BitmapFactory.Options bounds = new BitmapFactory.Options(); bounds.inJustDecodeBounds = true; BitmapFactory.decodeFile(image.getPath(), bounds); if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) { return null; } int originalSize = (bounds.outHeight &gt; bounds.outWidth) ? bounds.outHeight : bounds.outWidth; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = originalSize / 64; return BitmapFactory.decodeFile(image.getPath()); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload