使用Service()关闭我的Android应用程序后捕获屏幕

时间:2015-09-07 08:29:12

标签: android exception service android-service screenshot

我正在尝试在关闭应用程序后使用我的应用程序中的 Service()来捕获屏幕。

为此我有created Service() in onDestroy() and added android:stopWithTask="false" for that service in *manifest.xml*

但是我在调​​用它时遇到异常,如下面的那样::

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

我的Android代码就像这样::

protected void onDestroy()
{
    // TODO Auto-generated method stub
    super.onDestroy();
    showToast("Need to call Service here....");
    startService(new Intent(this, ScreenCaptureService.class));

}

和我的 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);
    }

}

1 个答案:

答案 0 :(得分:0)

我认为问题出现在你的尝试块

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

这是由于你的图片/位图没有得到绘制或捕获,我认为这是由于你试图捕获的xml / view。正如异常中明确指出的那样,它没有找到类和xml

  

无法创建服务   com.raju.mobile.shortcut.service.ScreenCaptureService:   android.content.res.Resources $ NotFoundException:来自xml类型的文件   布局资源ID#0x1020002

所以我的猜测是,问题在于:

 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View rootView = inflater.inflate(android.R.id.content, null).getRootView()
你能告诉我什么是android.R.id.content吗?

我认为当你的应用程序正在销毁时,所有东西都会从android进程中删除,所以当你的服务开始时,它会尝试膨胀xml并因此产生异常:(我不知道你究竟捕获了什么你想做什么)但我为你解决了问题。 ;