拍摄当前Android活动的快照?

时间:2013-12-10 07:56:50

标签: android

我需要在我的项目中为Android中的当前活动(整个主要布局)制作快照,我尝试了很多方法,但总是遇到错误“在cretenewFile上打开失败的eacces(权限被拒绝)“我的代码是这样的:

public void TakeSnapshoot(){
        mainlayout.setDrawingCacheEnabled(true);
        Bitmap b = mainlayout.getDrawingCache();

        String extr = Environment.getExternalStorageDirectory().toString()
                + "/SaveCapture";
       File myPath = new File(extr);

        if (!myPath.exists()) {
            boolean result = myPath.mkdir();
            Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
        }
        myPath = new File(extr, getString(R.string.app_name) + ".jpg");

        Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(myPath);

            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            MediaStore.Images.Media.insertImage(getContentResolver(), b,
                    "Screen", "screen");


        } catch (FileNotFoundException e) {
             e.printStackTrace();
            //Log.e("Error", e + "");
        }

        catch (Exception e) {
             e.printStackTrace();
            //Log.e("Error", e + "");
        }

}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="SOA.com"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
 <!-- Network State Permissions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- For android different screen sizes -->
    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:resizeable="true"
        android:anyDensity="true"
    /> ... 
拍完快照后,我可以打印出我想要的东西!?我认为这种方式是在android中打印当前活动的最佳方式吗?! 请任何想法或帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

添加

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在您的清单文件中。

希望这有帮助。

答案 1 :(得分:1)

这个函数会给你一个当前视图的位图。

Bitmap getBitmapFromView(View v) {
    v.getRootView().measure(
            MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,
                    MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(v.getLayoutParams().height,
                    MeasureSpec.EXACTLY));
    // v.getp
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
            Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
}