当我使用移动设备和移动设备主屏幕时,请截屏。当我使用消息传递,所有应用程序以及手机屏幕上的所有内容时,也要考虑。
排序时,拍摄“移动跑步”屏幕的图像:
1-Create layout
<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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Create ScreenShot" />
<ImageView
android:id="@+id/sowscreenshot"
android:layout_width="216dp"
android:layout_height="360dp"
android:layout_below="@+id/btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
</RelativeLayout>
2-Create Activity
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button btn_screenshoot;
int i = 0;
ImageView imgv_showscreenshot;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgv_showscreenshot = (ImageView) findViewById(R.id.showscreenshot);
btn_screenshoot = (Button) findViewById(R.id.btn);
btn_screenshoot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = findViewById(R.id.relativelayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
// 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()
+ "/ScreenShots");
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(
MainActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ i + ".jpg",
Toast.LENGTH_SHORT).show();
i++;
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
Toast.makeText(MainActivity.this,
"Sorry SD Card not available in your Device!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
3-Add permission to Manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句