更新 我添加了以下代码:
v1.setDrawingCacheEnabled(true);
v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v1.layout(0, 0, v1.getMeasuredWidth(), v1.getMeasuredHeight());
v1.buildDrawingCache(true);
现在我正在获取截图,但它们不是我需要的。例如,我需要捕获的一个屏幕包含一个包含ImageView和ListView的LinearLayout。在应用程序执行期间加载活动时,列表显示10个项目。我得到的捕获只显示了顶部的imageview和大约1.5个listitems。我能做些什么来准确捕捉屏幕显示的内容吗? (我会发布我正在拍摄的镜头,但由于知识产权问题,我必须基本上编辑所有内容,并且发布一个填充的黑色矩形并不是非常有用)
/更新
我的目标是Android 2.2并尝试开发一个可以从任何活动调用的类来捕获当前屏幕并将图像存储在SD卡上(最好是作为PNG)。我发现了类似的问题但这里有一个重要的区别:我无法创建一个新的View,我无法在每个Activity中构建这个代码(我可以在每个Activity中添加一个函数调用)。登记/> 所以我需要 1)获取当前Activity的视图(我正在使用((ViewGroup)findViewById(android.R.id.content))。getChildAt(0))和 2)将View传递给另一个类中的函数(在下面定义)。 我尝试过组合this和this,但在.getDrawingCache()调用之后,我得到的位图为空。所以.compress炸弹。
以下是代码:
public static void captureScreen(View v1){
View v1= v;
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
bm.compress(Bitmap.CompressFormat.PNG, 100, bytes);
} catch (Exception e1) {
Log.e("BM.Compress", e1.getMessage());
}
//create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + context.getPackageName().replace(".", File.separator)
+ File.separator + "screenshots" + File.separator + "test.jpg");
//Log.d("CF", context.getPackageName());
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//write the bytes in file
try {
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
logcat的:
06-06 16:37:14.613: D/AndroidRuntime(284): Shutting down VM
06-06 16:37:14.613: W/dalvikvm(284): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-06 16:37:14.768: E/AndroidRuntime(284): FATAL EXCEPTION: main
06-06 16:37:14.768: E/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{pkgname.pkgnameExpo/pkgname.pkgnameExpo.SplashActivity}: java.lang.NullPointerException: println needs a message
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.os.Looper.loop(Looper.java:123)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 16:37:14.768: E/AndroidRuntime(284): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 16:37:14.768: E/AndroidRuntime(284): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 16:37:14.768: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 16:37:14.768: E/AndroidRuntime(284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 16:37:14.768: E/AndroidRuntime(284): at dalvik.system.NativeStart.main(Native Method)
06-06 16:37:14.768: E/AndroidRuntime(284): Caused by: java.lang.NullPointerException: println needs a message
06-06 16:37:14.768: E/AndroidRuntime(284): at android.util.Log.println_native(Native Method)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.util.Log.e(Log.java:215)
06-06 16:37:14.768: E/AndroidRuntime(284): at pkgname.pkgnameExpo.Classes.CFunctions.captureScreen(CFunctions.java:432)
06-06 16:37:14.768: E/AndroidRuntime(284): at pkgname.pkgnameExpo.SplashActivity.onCreate(SplashActivity.java:38)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 16:37:14.768: E/AndroidRuntime(284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-06 16:37:14.768: E/AndroidRuntime(284): ... 11 more
我从调试中可以看出,在调用bm.compress之前,bm为null而v1不是。 有什么想法吗?
更新 这是新的LogCat,我认为它告诉我它无法创建文件?
06-06 22:13:55.975: I/System.out(12244): debugger has settled (1400)
06-06 22:13:58.351: E/BM.Compress(12244): nullfixnull
06-06 22:13:58.411: W/System.err(12244): java.io.IOException: No such file or directory
06-06 22:13:58.421: W/System.err(12244): at java.io.File.createNewFileImpl(Native Method)
06-06 22:13:58.432: W/System.err(12244): at java.io.File.createNewFile(File.java:1160)
06-06 22:13:58.432: W/System.err(12244): at pkgname.pkgnameExpo.Classes.CFunctions.captureScreen(CFunctions.java:441)
06-06 22:13:58.441: W/System.err(12244): at pkgname.pkgnameExpo.SplashActivity.onCreate(SplashActivity.java:38)
06-06 22:13:58.451: W/System.err(12244): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 22:13:58.451: W/System.err(12244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-06 22:13:58.461: W/System.err(12244): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-06 22:13:58.461: W/System.err(12244): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-06 22:13:58.471: W/System.err(12244): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-06 22:13:58.471: W/System.err(12244): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 22:13:58.491: W/System.err(12244): at android.os.Looper.loop(Looper.java:123)
06-06 22:13:58.491: W/System.err(12244): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 22:13:58.501: W/System.err(12244): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 22:13:58.511: W/System.err(12244): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 22:13:58.511: W/System.err(12244): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 22:13:58.522: W/System.err(12244): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 22:13:58.522: W/System.err(12244): at dalvik.system.NativeStart.main(Native Method)
06-06 22:13:58.531: W/System.err(12244): java.io.FileNotFoundException: /mnt/sdcard/pkgname/pkgnameExpo/screenshots/test.jpg (No such file or directory)
06-06 22:13:58.541: W/System.err(12244): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
06-06 22:13:58.551: W/System.err(12244): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
06-06 22:13:58.551: W/System.err(12244): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
06-06 22:13:58.561: W/System.err(12244): at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
06-06 22:13:58.561: W/System.err(12244): at pkgname.pkgnameExpo.Classes.CFunctions.captureScreen(CFunctions.java:447)
06-06 22:13:58.571: W/System.err(12244): at pkgname.pkgnameExpo.SplashActivity.onCreate(SplashActivity.java:38)
06-06 22:13:58.571: W/System.err(12244): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-06 22:13:58.581: W/System.err(12244): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-06 22:13:58.581: W/System.err(12244): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-06 22:13:58.591: W/System.err(12244): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-06 22:13:58.591: W/System.err(12244): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-06 22:13:58.601: W/System.err(12244): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 22:13:58.601: W/System.err(12244): at android.os.Looper.loop(Looper.java:123)
06-06 22:13:58.612: W/System.err(12244): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-06 22:13:58.612: W/System.err(12244): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 22:13:58.621: W/System.err(12244): at java.lang.reflect.Method.invoke(Method.java:521)
06-06 22:13:58.631: W/System.err(12244): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-06 22:13:58.631: W/System.err(12244): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-06 22:13:58.641: W/System.err(12244): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您似乎正在为您的文件构建一个相当深的路径名。如果所有这些目录都不存在,则需要在表示要将文件写入其中的目录的文件对象上调用mkdirs()。