I am trying to capturing screen using Service() from my application after closing my appliation.
For this I have created Service() in onDestroy() and added android:stopWithTask="false" for that service in *manifest.xml*
But i am getting exception while calling this like below::
09-07 17:16:59.095: E/AndroidRuntime(30566): FATAL EXCEPTION: main
09-07 17:16:59.095: E/AndroidRuntime(30566): Process: com.raju.mobile.shortcut, PID: 30566
09-07 17:16:59.095: E/AndroidRuntime(30566): java.lang.RuntimeException: Unable to create service com.raju.mobile.shortcut.service.ScreenCaptureService: android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020002
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2571)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.app.ActivityThread.access$1800(ActivityThread.java:138)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.os.Handler.dispatchMessage(Handler.java:102)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.os.Looper.loop(Looper.java:136)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.app.ActivityThread.main(ActivityThread.java:5034)
09-07 17:16:59.095: E/AndroidRuntime(30566): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 17:16:59.095: E/AndroidRuntime(30566): at java.lang.reflect.Method.invoke(Method.java:515)
09-07 17:16:59.095: E/AndroidRuntime(30566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-07 17:16:59.095: E/AndroidRuntime(30566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
09-07 17:16:59.095: E/AndroidRuntime(30566): at dalvik.system.NativeStart.main(Native Method)
09-07 17:16:59.095: E/AndroidRuntime(30566): Caused by: android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020002
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2356)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2311)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.Resources.getLayout(Resources.java:939)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-07 17:16:59.095: E/AndroidRuntime(30566): at com.raju.mobile.shortcut.service.ScreenCaptureService.takeScreenshot(ScreenCaptureService.java:67)
09-07 17:16:59.095: E/AndroidRuntime(30566): at com.raju .mobile.shortcut.service.ScreenCaptureService.onCreate(ScreenCaptureService.java:47)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
09-07 17:16:59.095: E/AndroidRuntime(30566): ... 10 more
09-07 17:16:59.095: E/AndroidRuntime(30566): Caused by: java.io.FileNotFoundException:
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.AssetManager.openXmlAssetNative(Native Method)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:488)
09-07 17:16:59.095: E/AndroidRuntime(30566): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2338)
09-07 17:16:59.095: E/AndroidRuntime(30566): ... 17 more
My Android code is like this::
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
showToast("Need to call Service here....");
startService(new Intent(this, ScreenCaptureService.class));
}
and my ScreenCaptureService.java
public class ScreenCaptureService extends Service
{
private static NotificationManager mNotificationManager;
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
//ShortcutActivity.showToast("Service Created");
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Bitmap capturedImageBitmap = takeScreenshot();
saveBitmap(capturedImageBitmap);
}
@Override
@Deprecated
public void onStart(Intent intent, int startId)
{
//ShortcutActivity.showToast("Service Started");
}
@Override
public void onDestroy()
{
// ShortcutActivity.showToast("Service Destroyed");
}
public Bitmap takeScreenshot()
{
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(android.R.id.content, null).getRootView();
// View rootView = ((Activity)getBaseContext()).getWindow().getDecorView().getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
@SuppressLint("SimpleDateFormat")
public void saveBitmap(Bitmap bitmap)
{
File imageDirPath = new File(Environment.getExternalStorageDirectory() + File.separator + "Screenhot");
Log.i("Raju", "Dir" + Environment.getExternalStorageDirectory() + File.separator + "Screenhot");
if (!imageDirPath.exists())
{
Log.i("Raju", "mkdir");
imageDirPath.mkdirs();
}
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
String imageName = formatter.format(date);
File imgPath = new File(imageDirPath + File.separator + "ScreenShot_" + imageName + ".jpg");
Log.i("Raju", "Image :" + imageDirPath + File.separator + imageName);
FileOutputStream fos;
try
{
fos = new FileOutputStream(imgPath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e)
{
Log.e("Raju", e.getMessage(), e);
}
catch (IOException e)
{
Log.e("Raju", e.getMessage(), e);
}
Notify(imgPath, bitmap);
}
private void Notify(File imgPath, Bitmap capturedImageBitmap)
{
// Create the style object with BigPictureStyle subclass.
NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
notiStyle.setBigContentTitle("Screenshot Captured");
notiStyle.setSummaryText("Screenshot Captured.");
// Add the big picture to the style.
notiStyle.bigPicture(capturedImageBitmap);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(imgPath), "image/*");
PendingIntent contentIntent = PendingIntent.getActivity(ScreenCaptureService.this.getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(ScreenCaptureService.this).setAutoCancel(true).setLargeIcon(capturedImageBitmap).setSmallIcon(R.drawable.icon).setContentIntent(contentIntent).setContentTitle("Screenshot Captured").setContentText("Touch here to view your screenshot").setStyle(notiStyle).build();
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mNotificationManager.notify(10, notification);
}
}