我需要使用子视图的屏幕快照关闭活动。
这是我的代码段:
private void returnBackWithSavedImage() {
int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
// Hide top/bottom sub views
updateView(View.GONE);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
parentImageRelativeLayout.setLayoutParams(layoutParams);
new CountDownTimer(1000, 500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
byte[] byteArray = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if (parentImageRelativeLayout != null) {
parentImageRelativeLayout.setDrawingCacheEnabled(true);
parentImageRelativeLayout.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
byteArray = byteArrayOutputStream .toByteArray();
}
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (Exception var7) {
var7.printStackTrace();
}
Intent returnIntent = new Intent();
if (byteArray != null) {
String imgString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
returnIntent.putExtra("image", imgString);
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}.start();
//Toast.makeText(this, getString(R.string.save_image_succeed), Toast.LENGTH_SHORT).show();
} else {
showPermissionRequest();
}
}
问题是调用了finish();
,但活动从未关闭。
如果我运行以下代码,它将关闭活动。
private void returnBackWithSavedImage() {
int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_OK, returnIntent);
finish();
} else {
showPermissionRequest();
}
}
我整天都在努力解决这个问题,但是我真的无法解决。
感谢您的帮助。
非常感谢您的时间和协助。
答案 0 :(得分:0)
只需将图像捕获过程移至某个方法即可解决该问题。 CountDownTimer
onFinish()
似乎没有在UI线程中运行。
因此,将您的代码更改为以下内容:
private void returnBackWithSavedImage() {
int permissionCheck = PermissionChecker.checkCallingOrSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
...
new CountDownTimer(1000, 500) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
captureImage();
}
}.start();
...
} else {
showPermissionRequest();
}
}
private void captureImage() {
byte[] byteArray = null;
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
if (parentImageRelativeLayout != null) {
parentImageRelativeLayout.setDrawingCacheEnabled(true);
parentImageRelativeLayout.getDrawingCache().compress(Bitmap.CompressFormat.JPEG, 80, byteArrayOutputStream);
byteArray = byteArrayOutputStream .toByteArray();
}
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (Exception var7) {
var7.printStackTrace();
}
Intent returnIntent = new Intent();
if (byteArray != null) {
String imgString = Base64.encodeToString(byteArray, Base64.NO_WRAP);
returnIntent.putExtra("image", imgString);
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}