view.getDrawingCache() only works once

I have a RelativeLayout with a loaded bitmap image using the Touch V2 example from Pragmatic Bookshelf — http://media.pragprog.com/titles/eband3/code/Touchv2/src/org/example/touch/Touch.java

I’ve added a separate button with onclicklistener that when clicked will load an image from the gallery. On the activity result the image is loaded as a bitmap into the RelativeLayout:

  • Android SharedPreference security
  • Download a file with no extension from a server
  • How to send HTML email
  • Fragment animation : difference between setCustomAnimations and setTransitionStyle
  • What video format will play on all Android devices?
  • Saving UI on orientation change - onSaveInstanceState not working as expected if retaining Fragment
  •     public void getPictureFromFile(Uri targetUri){
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = scale(getContentResolver()
                    .openInputStream(targetUri));
            workinprogress = BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(targetUri),
                    null, options);
            view.setImageBitmap(workinprogress);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    

    One the next button click, I grab the image of the relativelayout using:

                    thepicture.buildDrawingCache(true);
                Bitmap bm = Bitmap.createBitmap(thepicture.getDrawingCache());
    

    The process works terrific — for the first image. When I load another image again, the bitmap passed is still the same as the original. I’ve tried the thepicture.invalidate() and thepicture.resetDrawableState() before getDrawingCache() but neither seem to update the image to the newly loaded picture, although the frame layout displays the correct image.

    Is there something I don’t understand about refreshing drawingCache that I need to implement for the second image I load?

  • java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
  • Android and reflection
  • How to enable/disable toolbar scrolling programmatically when using design support library
  • How to determine if Android App open from Notification message?
  • FM radio app for Android
  • How do you check if music is playing by using a broadcast receiver?
  • One Solution collect form web for “view.getDrawingCache() only works once”

    To make it work more then once you have to use view.setDrawingCacheEnabled(true) each time before and view.setDrawingCacheEnabled(false) each time after calling view.getDrawingCache(). See the example:

    imageView.setDrawingCacheEnabled(true);
    imageView.buildDrawingCache(true);
    File imageFile = new File(Environment.getExternalStorageDirectory(),
            "Pictures/image.jpg");
    FileOutputStream fileOutputStream = new FileOutputStream(imageFile);
    imageView.getDrawingCache(true).compress(CompressFormat.JPEG, 100,
            fileOutputStream);
    fileOutputStream.close();
    imageView.setDrawingCacheEnabled(false);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.