问题
I've been trying to get this code working inside a fragment. This code runs fine inside an Activity, but I can't make it to work inside the fragment.
When I click a button, it calls the takeScreenshot method.
This is the code for the screenshot:
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
The "findViewById" it's not recognized due to being in a fragment.
I've tried:
View rootView = getView().findViewById(android.R.id.content).getRootView();
but it's not working either. I get
07-25 09:37:32.746: E/AndroidRuntime(31221): FATAL EXCEPTION: main
07-25 09:37:32.746: E/AndroidRuntime(31221): java.lang.NullPointerException
07-25 09:37:32.746: E/AndroidRuntime(31221): at com.dream3ncore.thefinalwordcounts.level01.Song01.takeScreenshot(Song01.java:763)
07-25 09:37:32.746: E/AndroidRuntime(31221): at com.dream3ncore.thefinalwordcounts.level01.Song01.onClick(Song01.java:152)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.view.View.performClick(View.java:4211)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.view.View$PerformClick.run(View.java:17362)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.os.Handler.handleCallback(Handler.java:725)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.os.Looper.loop(Looper.java:137)
07-25 09:37:32.746: E/AndroidRuntime(31221): at android.app.ActivityThread.main(ActivityThread.java:5227)
07-25 09:37:32.746: E/AndroidRuntime(31221): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 09:37:32.746: E/AndroidRuntime(31221): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 09:37:32.746: E/AndroidRuntime(31221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
07-25 09:37:32.746: E/AndroidRuntime(31221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
07-25 09:37:32.746: E/AndroidRuntime(31221): at dalvik.system.NativeStart.main(Native Method)
Any help will be greatly appreciated.
回答1:
Your Fragment
's UI is a subset of your Activity
's UI. getView()
in a Fragment
will return only its own view hierarchy. You could just change your call to getView()
to getActivity()
, and that would work. Alternately, I believe you could just call getView().getRootView()
and avoid the lookup of android.R.id.content
in the first place.
回答2:
In the onCreateView method where the fragments view is inflated. That is the view that I tie the screenshot code too.
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache()));
view.setDrawingCacheEnabled(false);
For those that did not tie the screenshot to an action, I had to create a postDelay handler that checked with the fragment was ready. I used something like this
private void waitForView() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (isVisible()) {
// create the view snapshot
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap screenshot = Bitmap.createBitmap(view.getDrawingCache()));
view.setDrawingCacheEnabled(false);
} else {
waitForView();
}
}
}, 100);
}
This worked relatively well for the fragment and handling views that took time to generate.
来源:https://stackoverflow.com/questions/24944480/take-screenshot-method-inside-fragment