簡體   English   中英

使用Service()關閉我的Android應用程序后捕獲屏幕

[英]Capture Screen after close my Android application using Service()

關閉應用程序后,我試圖使用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);
    }

}

我認為問題出在您的try塊中

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進行充氣並因此產生異常:(我不知道您到底在捕獲什么,您是什么)嘗試做),但我為您解決了問題。 ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM