gpt4 book ai didi

java - 以编程方式截取屏幕截图

转载 作者:行者123 更新时间:2023-12-02 05:55:21 25 4
gpt4 key购买 nike

在这里,您可以查看以编程方式拍摄屏幕截图并在 ImageView 中显示屏幕截图,并检查天气 SD 卡是否在您的设备中,将屏幕截图保存到 SD 卡中。

Xml(main.xml)

在main.xml中声明所有 View 对象。

<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="Click Here to Create ScreenShot" />

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

</RelativeLayout>

Android Activity (ScreenshotActivity.java)

在此 Activity 中,我们可以看到当用户单击按钮时以编程方式截取屏幕截图。

package com.androidsurya.screenshot;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

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.main);

btn_screenshoot = (Button) findViewById(R.id.btn_screenshoot);
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);
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()
+ "/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(
ScreenshotActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ 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();
}

}
});

}

}

AndroidManifest.xml:

在 list 文件中将屏幕截图存储在SD卡中是强制性的。

<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" />

<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>
</application>

</manifest>

After a lot of searching finally this link helps.

最佳答案

将其放在 OnCreate

Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
int height = "ParentLayoutOFView".getMeasuredHeight();

createImage(height, width, linearLayout, "FileName");

添加此方法

public File createImage(int height, int width, View view, String fileName) {
Bitmap bitmapCategory = getBitmapFromView(view, height, width);
return createFile(bitmapCategory, fileName);
}

public File createFile(Bitmap bitmap, String fileName) {

File externalStorage = Environment.getExternalStorageDirectory();
String sdcardPath = externalStorage.getAbsolutePath();
File reportImageFile = new File(sdcardPath + "/YourFolderName" + fileName + ".jpg");
try {
if (reportImageFile.isFile()) {
reportImageFile.delete();
}
if (reportImageFile.createNewFile()) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = new FileOutputStream(reportImageFile);
fo.write(bytes.toByteArray());
bytes.close();
fo.close();

return reportImageFile;
}
} catch (Exception e) {
Toast.makeText(ReportsActivity.this, "Unable to create Image.Try again", Toast.LENGTH_SHORT).show();
}
return null;
}

public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {

Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);

view.measure(MeasureSpec.makeMeasureSpec(totalWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
view.layout(0, 0, totalWidth, totalHeight);
view.draw(canvas);
return returnedBitmap;
}

关于java - 以编程方式截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23148296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com