当我的应用程序截取屏幕截图时,它会返回黑屏或黑色背景,而不是实际屏幕截图

时间:2015-04-13 19:46:17

标签: android background screenshot

我有一个应用程序,它截取屏幕截图并保存它们正常工作。但是,现在当我去查看屏幕截图时,它既有黑色背景,也只是显示黑屏。如何返回我的应用程序的实际屏幕截图?

MainActivity

public class ScreenshotActivity extends Activity {

    Button btn_screenshoot;
    int i = 0;
    ImageView imgv_showscreenshot;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screenshotlayout);

        btn_screenshoot = (Button) findViewById(R.id.btn_screenshoot);
        btn_screenshoot.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
                View view = findViewById(R.id.relativelayout);
                view.setDrawingCacheEnabled(true);
                Bitmap bitmap = view.getDrawingCache();
                BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                imgv_showscreenshot = (ImageView) findViewById(R.id.imgv_showscreenshot);
                // set screenshot bitmapdrawable to imageview
                imgv_showscreenshot.setBackgroundDrawable(bitmapDrawable);

                if (Environment.MEDIA_MOUNTED.equals(Environment
                        .getExternalStorageState())) {
                    // we check if external storage is available, otherwise
                    // display an error message to the user using Toast Message
                    File sdCard = Environment.getExternalStorageDirectory();
                    File directory = new File(sdCard.getAbsolutePath()
                            + "/DCIM/Images");
                    directory.mkdirs();

                    String filename = "screenshot" + i + ".jpg";
                    File yourFile = new File(directory, filename);

                    while (yourFile.exists()) {
                        i++;
                        filename = "screenshot" + i + ".jpg";
                        yourFile = new File(directory, filename);
                    }

                    if (!yourFile.exists()) {
                        if (directory.canWrite()) {
                            try {
                                FileOutputStream out = new FileOutputStream(
                                        yourFile, true);
                                bitmap.compress(Bitmap.CompressFormat.PNG, 90,
                                        out);
                                out.flush();
                                out.close();
                                Toast.makeText(
                                        ScreenshotActivity.this,
                                        "File exported to: " + yourFile.getAbsolutePath()+ i + ".jpg",
                                        Toast.LENGTH_SHORT).show();
                                i++;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }

                } else {
                    Toast.makeText(ScreenshotActivity.this,
                            "Sorry SD Card not available in your Device!",
                            Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btn_screenshoot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Capture" />

    <ImageView
        android:id="@+id/imgv_showscreenshot"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_below="@+id/btn_screenshoot"
        android:layout_marginTop="10dp" />

</RelativeLayout>

Android Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.screenshot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidsurya.screenshot.ScreenshotActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

检查您的手机是否已植根。如果您的手机已植根,请尝试使用

进程sh = Runtime.getRuntime()。exec(&#34; su&#34;,null,null);

                OutputStream  os = sh.getOutputStream();
                os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
                os.flush();

                os.close();
                sh.waitFor();

然后将img.png作为位图读取并将其转换为jpg如下

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+File.separator +"img.png");

//my code for saving
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.

File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput

fo.close();

如果您的应用程序处于后台,除非您扎根,否则您无权访问该屏幕,即使您在后台,上面的代码也可以最有效地截取屏幕截图。

更新

谷歌有一个图书馆,你可以用它来截取而不需要生根,我试过了,但我确信它会尽快消耗内存。

尝试http://code.google.com/p/android-screenshot-library/

取自Android - How to take screenshot programatically