Updating a bitmap ImageView inside a fragment

Part of my application uses a ViewPager with three fragments as a โ€œWizardโ€ to create an event. The first fragment contains the default photo in ImageView and a button that launches AlertDialog, allowing the user to either take a new photo or use an existing photo to communicate with the event.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/testing" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:text="Update Crawl Image" android:onClick="fireImageDialog"/> <ImageView android:id="@+id/imageView1" android:layout_width="278dp" android:layout_height="284dp" android:layout_gravity="center" android:src="@android:drawable/alert_light_frame" /> </LinearLayout> 

Once the user has selected a photo, I would like it to replace the default image. fireImageDialog returns the path to the selected image either from Intent.ACTION_PICK OR MediaStore.ACTION_IMAGE_CAPTURE , as expected, that is, I confirmed that the returned path is a valid path to the image. At the end of my onActivityResult method onActivityResult I retrieve the fragment containing the above code and call the method to set the ImageView bitmap.

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == LOAD_CRAWL_IMAGE) { if (resultCode == RESULT_OK) { // Some code here to handle the two potential sources of the path // sets `selectedImagePath` ((NewCrawlBasicDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.newCrawlBasicDetails)).UpdateImage(selectedImagePath); } else if (resultCode != RESULT_CANCELED) { Toast.makeText(this, "Failed to load image", Toast.LENGTH_LONG) .show(); } } } 

and its associated UpdateImage method:

 public void UpdateImage(String path){ if (path.length() > 0) { ImageView myPhoto = (ImageView) getView().findViewById(R.id.imageView1); myPhoto.setImageBitmap(decodeSampledBitmapFromFile( path, myPhoto.getWidth(), myPhoto.getHeight())); } } 

My problem is that after all this, ImageView still shows the original image by default. I tried calling myPhoto.invalidate() to force the view to re-draw, but no luck.

Any suggestions on what might happen?

+6
source share

All Articles