1

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

}
Raju-san
  • 166
  • 1
  • 2
  • 12

1 Answers1

0

I think the problem is occurring in your try block

try
        {
            fos = new FileOutputStream(imgPath);
            bitmap.compress(CompressFormat.PNG, 100, fos);
            fos.flush();
            fos.close();
        }

and it is due to your picture/bitmap is not getting draw or captured, And I think this is due to xml/view you are trying to capture. As in exception it is clearly says that it does not found the class and xml

Unable to create service com.raju.mobile.shortcut.service.ScreenCaptureService: android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020002

so my guess is , the problem is here :

 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(android.R.id.content, null).getRootView()

can you tell me what is android.R.id.content ??

I think as your application is destroying , so all thing is removed from the android process , so when your service get started , it tries to inflate the xml and hence producing the exception : (I dont know what exactly you are capturing and what you are trying to do ) But I sort out the problem for you. ;

Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID [link]more details[/link](http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity) – Raju-san Sep 07 '15 at 08:43
  • what you are trying to do – Coas Mckey Sep 07 '15 at 08:56
  • and as I mentioned above it could be the exception in try block , so in try block the path could not be found may be , check that please – Coas Mckey Sep 07 '15 at 08:57
  • in your case what is content ? – Coas Mckey Sep 07 '15 at 08:57
  • I am not getting any problem with my *try* block. I am getting exception at **View rootView = inflater.inflate(android.R.id.content, null).getRootView()**. I am trying to capture the screen after closing my current application – Raju-san Sep 07 '15 at 09:00
  • so what is your content – Coas Mckey Sep 07 '15 at 09:06
  • it should be the id of your view you want to capture – Coas Mckey Sep 07 '15 at 09:07
  • The value passed to the `inflate` method should be a layout, not an id. That's why it's crashing. – Kane O'Riley Sep 07 '15 at 11:29
  • Also, what that is doing is in no way close to resembling code that will be able to retrieve the current activity screenshot. My previous comment will fix your crash, but you won't get the image you're looking for. That will just inflate a new layout and the drawing cache will probably be blank since its not being drawn to the screen. – Kane O'Riley Sep 07 '15 at 11:31