采取截图来自活动截图

2023-09-06 16:17:59 作者:时间# 淡却的是情还是心

我试图用这个code从活动采取截图,但我在与code线得到一个显示java.lang.NullPointerException:

 位图B = Bitmap.createBitmap(view.getDrawingCache(),0,statusBarHeight,宽度,高度 -  statusBarHeight) 

,当我试图调试我发现,所有的变量都无法解决;即,statusBarHeight,宽度,高度。请帮我解决这个。

 活动AV;查看视图。@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    鉴于=(查看)findViewById(R.id.screen);    AV = HelloAndroidActivity.this;    尝试{    takeScreenShot(AV);    }    赶上(例外五)    {}}公共无效takeScreenShot(活动AV)抛出异常{    查看= this.getWindow()getDecorView();    //要么    //视图= av.getWindow()getDecorView()。    view.setDrawingCacheEnabled(真);    view.buildDrawingCache(真);    位图B1 = view.getDrawingCache();    矩形框=新的矩形();    。this.getWindow()getDecorView()getWindowVisibleDisplayFrame(帧)。    INT statusBarHeight = frame.top;    INT宽度= this.getWindowManager()getDefaultDisplay()的getWidth()。    INT高度= this.getWindowManager()getDefaultDisplay()的getHeight()。    位图B = Bitmap.createBitmap(view.getDrawingCache(),0,statusBarHeight,宽度,高度 -  statusBarHeight);    view.destroyDrawingCache();    FOS的FileOutputStream = NULL;    尝试    {        //文件sddir =新的文件(路径名);        文件根= Environment.getExternalStorageDirectory();        // S = sddir.toString();        Toast.makeText(这一点,路径+根,Toast.LENGTH_LONG).show();        如果(!root.exists())        {            root.mkdirs();        }        FOS =新的FileOutputStream(根+/图片+_+4+.JPEG);        如果(FOS!= NULL)        {            b.com preSS(Bitmap.Com pressFormat.JPEG,90,FOS);            fos.flush();            fos.close();        }    }    赶上(例外五)    {        Toast.makeText(这一点,异常+ E,Toast.LENGTH_LONG).show();        Log.e(,而动态图像时出现异常:e.toString());        e.printStackTrace();    }} 

解决方案 揭秘2018 雷霆行动之台湾谍案 小哲

使用以下code避免的无效的位图:

 公共类AndroidWebImage延伸活动{ImageView的bmImage;观的LinearLayout;  / **当第一次创建活动调用。 * /  @覆盖  公共无效的onCreate(捆绑savedInstanceState){      super.onCreate(savedInstanceState);      的setContentView(R.layout.main);      鉴于=(的LinearLayout)findViewById(R.id.screen);      bmImage =(ImageView的)findViewById(R.id.image);      view.setDrawingCacheEnabled(真);      //这是最重要的code :)      //没有它的视图将具有0,0维和位图将为空      view.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),            MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));      view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());      view.buildDrawingCache(真);      位图B = Bitmap.createBitmap(view.getDrawingCache());      view.setDrawingCacheEnabled(假); //明确绘图缓存      bmImage.setImageBitmap(二);};} 

好好看看,你必须使用:

  view.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED)                MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED)); view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight()); 

我用下面的 main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android  机器人:方向=垂直   机器人:ID =@ + ID /屏幕  机器人:layout_width =FILL_PARENT  机器人:layout_height =FILL_PARENT  ><的TextView  机器人:layout_width =FILL_PARENT  机器人:layout_height =WRAP_CONTENT  机器人:文字=@字符串/你好  />< ImageView的  机器人:ID =@ + ID /图像  机器人:layout_width =FILL_PARENT  机器人:layout_height =FILL_PARENT/>< / LinearLayout中> 

的结果是:

Reference

I am trying to use this code to take a screenshot from the activity but I am getting a java.lang.nullpointerexception at a line with the code:

Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight)

and when I tried to debug I found that all the variables are not able to resolve; i.e., statusBarHeight, width, height. Please help me in resolving this.

Activity av; 
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = (View)findViewById(R.id.screen);
    av= HelloAndroidActivity.this;

    try {   
    takeScreenShot(av);
    }
    catch (Exception e) 
    { }
}  

public void takeScreenShot(Activity av) throws Exception 
{ 
    view =this.getWindow().getDecorView();
    //or
    //view =av.getWindow().getDecorView();

    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(true);           

    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = this.getWindowManager().getDefaultDisplay().getWidth();
    int height = this.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();

    FileOutputStream fos = null; 

    try 
    { 
        //File sddir = new File(path, name);
        File root = Environment.getExternalStorageDirectory();

        //s= sddir.toString();
        Toast.makeText(this, "path " + root, Toast.LENGTH_LONG).show();

        if (!root.exists()) 
        { 
            root.mkdirs(); 
        }               

        fos = new FileOutputStream(root+ "/Image" + "_" + "4" + ".jpeg");              

        if (fos != null) 
        { 
            b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
            fos.flush();
            fos.close();                  
        } 
    } 
    catch (Exception e) 
    { 
        Toast.makeText(this, "Exception " + e, Toast.LENGTH_LONG).show();
        Log.e("Exception occurred while moving image: ",  e.toString());
        e.printStackTrace();
    } 
} 

解决方案

Use the following code to avoid a null bitmap:

public class AndroidWebImage extends Activity {

ImageView bmImage; 
LinearLayout view;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      view = (LinearLayout)findViewById(R.id.screen);
      bmImage = (ImageView)findViewById(R.id.image);

      view.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          

      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

      view.buildDrawingCache(true);
      Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache

      bmImage.setImageBitmap(b);   

};


}

Take a good look, you have to use:

 view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

I used the following main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
   android:id="@+id/screen"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<ImageView
  android:id="@+id/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
/>
</LinearLayout>

The outcome is:

Reference

 
精彩推荐
图片推荐