我想拍摄自定义警报视图的图像。我已经使用 buildDrawingCache 将 LinearLayout 转换为位图并且工作正常,但是当我尝试将自定义警报视图(使用 RelativeLayout)转换为位图时,它给了我一个错误。
//将视图转换为图像
private String getLayoutBitmap() {
RelativeLayout view = (RelativeLayout) findViewById(R.id.glayer);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bikeBm = view.getDrawingCache();
File filePath = getFileStreamPath("twitterimage1.jpg");
// delete saved image
if (new File(filePath.toString()).delete()) {
Log.i("INTERNAL_STORAGE_IMG_FILE", "deleted");
}
try {
// Use the compress method on the Bitmap object to write image to
// the OutputStream
FileOutputStream fos = openFileOutput("twitterimage1.jpg",
Context.MODE_WORLD_READABLE);
// Writing the bitmap to the output stream
bikeBm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
return filePath.toString();
}
//例外
02-22 11:18:37.758: E/AndroidRuntime(21822): FATAL EXCEPTION: main
02-22 11:18:37.758: E/AndroidRuntime(21822): java.lang.NullPointerException
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.bike.app.ScoreClass.getLayoutBitmap(ScoreClass.java:435)
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.bike.app.ScoreClass.initShareIntent(ScoreClass.java:475)
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.bike.app.ScoreClass.access$0(ScoreClass.java:460)
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.bike.app.ScoreClass$2.onClick(ScoreClass.java:156)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.view.View.performClick(View.java:3620)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.view.View$PerformClick.run(View.java:14292)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.os.Handler.handleCallback(Handler.java:605)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.os.Looper.loop(Looper.java:137)
02-22 11:18:37.758: E/AndroidRuntime(21822): at android.app.ActivityThread.main(ActivityThread.java:4507)
02-22 11:18:37.758: E/AndroidRuntime(21822): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 11:18:37.758: E/AndroidRuntime(21822): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-22 11:18:37.758: E/AndroidRuntime(21822): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-22 11:18:37.758: E/AndroidRuntime(21822): at dalvik.system.NativeStart.main(Native Method)