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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With