Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

    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?

like image 650
kittka Avatar asked Jun 11 '12 23:06

kittka


1 Answers

To make it work more than 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);
like image 152
Dmytro Titov Avatar answered Oct 14 '22 21:10

Dmytro Titov