Android 截取(截畫面)本身APP 執行畫面

Android 截取(截畫面)本身APP 執行畫面

Android 截取(截畫面)本身APP
執行畫面


 

XML

<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”

   
xmlns:tools=”http://schemas.android.com/tools”

   
android:layout_width=”match_parent”

   
android:layout_height=”match_parent”

   
tools:context=”${packageName}.${activityClass}”

   
android:background=”#FFFFFF”

   
android:orientation=”vertical”>

   

   
<Button

        android:id=”@+id/button01″

       
android:layout_width=”fill_parent”

       
android:layout_height=”wrap_content”

        android:text=”截取當前螢幕

        />

 

   
<TextView

        android:id=”@+id/textview”

       
android:layout_width=”wrap_content”

       
android:layout_height=”wrap_content”

       
android:layout_marginTop=”100.0dip”

        android:text=”當前螢幕

        android:textSize=”20.0sp”

     
  android:textColor=”#FFFF0000″

        android:gravity=”center”

       
android:layout_gravity=”center_horizontal” />

 

</LinearLayout>

 

Code

package com.example.android_screen;

 

import java.io.File;

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

 

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.os.Bundle;

import android.os.Environment;

import android.view.Display;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.Toast;

 

public class MainActivity extends
Activity {

  private
Button button01;

  public
Context m_context;

   
@Override

   
protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

       
setContentView(R.layout.activity_main);

        initView();

        m_context=(Context)this;

        button01.setOnClickListener(new
View.OnClickListener() {

      

   
      @Override

   
      public void onClick(View v)
{

   
         // TODO Auto-generated
method stub

   
         button01.setVisibility(Button.GONE);//
截圖開始時隱藏按鈕

   
         getAndSaveCurrentImage();

   
         button01.setVisibility(Button.VISIBLE);//
截圖結束時顯示按鈕

   
      }

   
    });

   
}

 

 
//
初始化組件

   
public void initView(){

   
button01 =
(Button)this.findViewById(R.id.button01);

   
}

   

   
@Override

   
public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items
to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main,
menu);

        return true;

   
}

 
//
截取當前螢幕的圖像並保存至SDCard特定目錄下

   
public void getAndSaveCurrentImage(){

   
WindowManager windowManager =
this.getWindowManager();

   
Display display =
windowManager.getDefaultDisplay();

   
int w = display.getWidth();

   
int h = display.getHeight();

   
Bitmap bitmap =
Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

   
View decorView =
this.getWindow().getDecorView();

   
decorView.setDrawingCacheEnabled(true);

 
   bitmap = decorView.getDrawingCache();

   
String savePath =
getSDCardPath() + “/mcc/currentImage/”;

   

   
try{

   
    File path = new
File(savePath);

   
    SimpleDateFormat sdf = new
SimpleDateFormat(“yyyy-MM-dd_HH_mm_ss”, Locale.US);

   
    String imagePath = savePath +
sdf.format(new Date()) + “.png”;

   
    File file = new
File(imagePath);

   
    if(!path.exists()){

   
      path.mkdirs();

   
    }

   
    if(!file.exists()){

   
      file.createNewFile();

   
    }

   
    FileOutputStream fos = null;

   
  fos
= new FileOutputStream(file);

   
    if(null != fos){

   
      bitmap.compress(Bitmap.CompressFormat.PNG,
90, fos);

   
      fos.flush();

   
      fos.close();

   
      Toast.makeText(this, ”
截屏檔已保存至SDCard/mcc/currentImage/“, 

                Toast.LENGTH_LONG).show();

   
    }

   
}catch(Exception e){

   
    e.printStackTrace();

   
}

   
}

   

   
//
得到SDCard根目錄

   
public String getSDCardPath(){

     File
sdCardDir = null;

    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

       sdCardDir
= Environment.getExternalStorageDirectory();

     }

    

   
return sdCardDir.toString();

   

   
}   

}

 


 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *