onClick takeScreenShot导致暂停

时间:2014-01-22 20:04:43

标签: java android debugging

以下更新

应用程序继续暂停。我试图通过点击按钮获取屏幕截图。单击按钮时它会暂停。这是我在调试透视图中得到的结果:

MainActivity$1.onClick(View) line: 108  
Button(View).performClick() line: 4438  
View$PerformClick.run() line: 18422 
Handler.handleCallback(Message) line: 733   
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 95 
Looper.loop() line: 136 
ActivityThread.main(String[]) line: 5017    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not   available [native method]    
Method.invoke(Object, Object...) line: 515  
ZygoteInit$MethodAndArgsCaller.run() line: 779  
ZygoteInit.main(String[]) line: 595 
NativeStart.main(String[]) line: not available [native method]

我使用onClick拍摄屏幕截图如下所示:

            public void onClick(View view) {




            Activity MainActivity = null;
  //Line 108  View view1 = MainActivity.getWindow().getDecorView();
             view1.setDrawingCacheEnabled(true);
             view1.buildDrawingCache();
             Bitmap b1 = view1.getDrawingCache();
             Rect frame = new Rect();
             MainActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
             int statusBarHeight = frame.top;
             int width = MainActivity.getWindowManager().getDefaultDisplay().getWidth();
             int height = MainActivity.getWindowManager().getDefaultDisplay().getHeight();

             Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
             view1.destroyDrawingCache();

             FileOutputStream fos = null;
             try
             {
                 fos = new FileOutputStream(strFileName);
                 if (null != fos)
                 {
                     b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                     fos.flush();
                     fos.close();
                 }
             }
             catch (FileNotFoundException e)
             {
                 e.printStackTrace();
             }
             catch (IOException e)
             {
                 e.printStackTrace();
             }



        }

----------------------------------- UPDATE ----------- -------------------------------

我从onClick()中取出了这个方法。我在这里创建了第二个名为ScreenShot.java的活动:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

public class ScreenShot extends MainActivity {


private static Bitmap takeScreenShot(Activity MainActivity)
{
    View view = MainActivity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    MainActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = MainActivity.getWindowManager().getDefaultDisplay().getWidth();
    int height = MainActivity.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
    view.destroyDrawingCache();
    return b;
}
private static void savePic(Bitmap b, String strFileName)
{
    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream(strFileName);
        if (null != fos)
        {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
}

然后我想在MainActivity的onClick()中启动ScreenShot.class:

Intent myIntentg = new Intent(view.getContext(), ScreenShot.class);
             startActivityForResult(myIntentg, 0);

我还将新的ScreenShot.java添加到清单。

现在唯一发生的事情是我当前的活动类型闪现并重新开始,但没有截屏或其他任何事情发生。

1 个答案:

答案 0 :(得分:1)

没有理由发起不同的意图。在你的onclick方法中,只需调用实际完成工作的方法。

  onClick(View view) {
        new Thread(new Runnable() {
            public void run() {
                Call your methods
            }
        }).start();
 }