gpt4 book ai didi

android - 在第二个 Activity 中显示捕获的屏幕截图(在 ImageView 或图库中)

转载 作者:行者123 更新时间:2023-11-29 17:27:32 27 4
gpt4 key购买 nike

我的代码是从第一个 Activity 中捕获屏幕截图。在我的第一个 Activity 中,当我单击按钮时,它将捕获屏幕并将其保存到手机中,并在第二个 Activity 中显示 ImageView 或图库中的所有图像。

第一个 Activity :

Button btn;

ImageView img;
Bitmap bitmap;

int j = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen__main);
//final BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imgv_capturedImage = (ImageView) findViewById(R.id.imageView1);
img = (ImageView) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.btn_screenshoot);
btn .setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
captureScreen(v);
saveImage(bitmap);

Log.d("SCREEN", "ONCLICK");
Intent i = new Intent(getApplicationContext(),B.class);
i.putExtra("image",bitmap);
startActivity(i);
}
});
}


public void captureScreen(View v1){
Log.d("SCREEN", "CAPTURE");
View rootview = v1.getRootView();
rootview.setDrawingCacheEnabled(true);
bitmap = rootview.getDrawingCache();

}

public void saveImage(Bitmap b){
Log.d("SCREEN", "SAVE");
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/ScreenShots");
directory.mkdirs();

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

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

if (!yourFile.exists()) {
if (directory.canWrite()) {
try {
FileOutputStream out = new FileOutputStream(
yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 80,
out);
out.flush();
out.close();
Toast.makeText(Screen_MainActivity.this,"File exported to /sdcard/ScreenShots/screenshot"+ j + ".jpg",
Toast.LENGTH_SHORT).show();
j++;
} catch (IOException e) {
e.printStackTrace();
}
}
}

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

}
}

第二个 Activity :

Screen_MainActivity sc;
ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("SCREEN", "B");
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.b);
Intent i = new Intent();
Bitmap bitmap = (Bitmap)i.getParcelableExtra("image");
imgView = (ImageView) findViewById(R.id.imageView1);

imgView.setImageBitmap(bitmap);
}
}

最佳答案

将文件的路径发送到第二个 Activity 而不是发送整个位图。并在第二个 Activity 中检索它。

关于android - 在第二个 Activity 中显示捕获的屏幕截图(在 ImageView 或图库中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128812/

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