Android截屏且保存至SD卡

jopen 10年前
//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;        }    }