Android截屏且保存至SD卡
//main.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button"
android:text="click here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello every day" />
</LinearLayout> //MainActivity如下:
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
//出現的問題的描述:
//當點擊按鈕后將生產的圖片會被保存到SD卡中,此時若把圖片從SD中導出至桌面
//看到的圖片是沒有預覽的,相當于圖片是空白的.
//解決辦法:
//將此圖片從SDCard中刪除,或者關閉模擬器.此時桌面上的圖片顯示正常
//原因:
//圖片同時被桌面和手機模擬器占用
public class ScreenTestActivity extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListenerImpl());
}
class ButtonClickListenerImpl implements View.OnClickListener {
@Override
public void onClick(View v) {
getCurrentScreen();
}
}
// 截取屏幕
public void getCurrentScreen() {
// 1.構建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();//w=480
int h = display.getHeight();//h=800
Bitmap imageBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);//最后一個參數叫位圖結構
//ARGB--Alpha,Red,Green,Blue.
//ARGB為一種色彩模式,也就是RGB色彩模式附加上Alpha(透明度)通道,常見于32位位圖的存儲結構。
// 2.獲取屏幕
View decorview = this.getWindow().getDecorView();//decor意思是裝飾布置
decorview.setDrawingCacheEnabled(true);
imageBitmap = decorview.getDrawingCache();
String SaveImageFilePath = getSDCardPath() + "/gameCounter";//保存圖片的文件夾路徑
// 3.保存Bitmap
try {
File path = new File(SaveImageFilePath);
String imagepath = SaveImageFilePath + "/Screen_" + ".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) {
//imageBitmap.compress(format, quality, stream);
//把位圖的壓縮信息寫入到一個指定的輸出流中
//第一個參數format為壓縮的格式
//第二個參數quality為圖像壓縮比的值,0-100.0 意味著小尺寸壓縮,100意味著高質量壓縮
//第三個參數stream為輸出流
imageBitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(this,"圖片已經已保存至"+SaveImageFilePath,Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 獲取SDCard的目錄路徑功能
*/
private String getSDCardPath() {
String SDCardPath = null;
// 判斷SDCard是否存在
boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if (IsSDcardExist) {
SDCardPath = Environment.getExternalStorageDirectory().toString();//SD卡的路徑為: /mnt/sdcard
}
return SDCardPath;
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!