我想让我的应用程序能够像我自己的设备那样只需按一下按钮即可获得正确的屏幕截图,但它似乎只是截取视图的截图。我想要的是能够像我的设备那样使用屏幕上的任何内容获取完整的屏幕截图。
这是我的java代码:
package com.arvisapps.lazyscreenshot;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
RelativeLayout R1;
ImageView img;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
private void showNotification() {
builder.setAutoCancel(false);
builder.setContentTitle("Take Screenshot");
builder.setContentText("");
builder.setSmallIcon(R.drawable.ic_notification);
builder.setOngoing(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(1, notification);
}
private void dontShowNotification(){
NotificationManager nMgr = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
nMgr.cancel(1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
R1 = (RelativeLayout) findViewById(R.id.relatv);
CheckBox cb1 = (CheckBox) findViewById(R.id.cb_1);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
showNotification();
} else if (isChecked == false) {
dontShowNotification();
}
}
});
}
public void takeScreenshot(View v)
{
View v1 = R1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bmDrawable = new BitmapDrawable(bm);
img = (ImageView) findViewById(R.id.screenshots);
img.setBackgroundDrawable(bmDrawable);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relatv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/cb1_grp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/cb1_txtvw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/cb1_txt" />
<CheckBox
android:id="@+id/cb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/cb1_txtvw"
android:layout_marginLeft="107dp"
android:layout_marginStart="107dp"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/edittxt_grp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/cb1_grp"
android:layout_centerHorizontal="true">
<TextView
android:id="@+id/edittxt_txtvw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delaytime" />
<EditText
android:id="@+id/edittxt_delay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"/>
</LinearLayout>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edittxt_grp"
android:layout_centerHorizontal="true"
android:text="@string/btn1_txt"
android:onClick="takeScreenshot"/>
<ImageView
android:layout_below="@id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/screenshots"
android:contentDescription="@string/app_name"
/>
答案 0 :(得分:1)
如果您的手机是root用户,请尝试。
进程sh = Runtime.getRuntime()。exec(“su”,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();
答案 1 :(得分:0)
您正在使用的方法getDrawingCache()将只获取该视图的绘图缓存。遗憾的是,除非您的设备已植根,否则应用程序无法直接访问帧缓冲区(以及原始像素),因此您无法从应用中获取完整屏幕截图。