Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...