1.注意:
需要回调时访问事件的方法-registerForActivityResult必须在生命周期STARTED之前调用,及即在onCreate中创建
2.如拍照例子,简单回调,可在回调方法中对图片进行各种处理。
a.Activity
package tian.project.easytostop.activity;
import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.IOException;
import tian.project.easytostop.MainActivity;
import tian.project.easytostop.R;
/**
* @author tian
* @date 2022.6.17
* @describe 拍照页面添加停车位的弹框显示活动
*
*/
public class DialogActivity extends AppCompatActivity {
//拍照
private static final int TAKE_PHOTO = 1;
//获取相册的图片
private static final int CHOOSE_PHOTO = 2;
//uri对象
private Uri imageUri;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//需要回调时访问事件的方法-registerForActivityResult必须在生命周期STARTED之前调用
ActivityResultLauncher<Intent> intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
Intent data = result.getData();
System.out.println("回调数据" + data);
});
//开启相机权限
ActivityCompat.requestPermissions(
DialogActivity.this,
new String[]{Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
1);
// showBottomDialog();
//1、使用Dialog、设置style
final Dialog dialog = new Dialog(this, R.style.DialogTheme);
//2、设置布局
View view = View.inflate(this, R.layout.dialog, null);
dialog.setContentView(view);
Window window = dialog.getWindow();
//设置弹出位置
window.setGravity(Gravity.BOTTOM);
//设置弹出动画
window.setWindowAnimations(R.style.main_menu_animStyle);
//设置对话框大小
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();
dialog.findViewById(R.id.tv_take_photo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("DialogActivity", "onCreate: 实现拍摄!");
dialog.dismiss();
System.out.println("实现拍照的照片!");
// // 创建File对象,用于存储拍照后的图片
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
System.out.println("------------------------01" + getExternalCacheDir());
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("SDK版本:" + Build.VERSION.SDK_INT);
if (Build.VERSION.SDK_INT < 24) {
imageUri = Uri.fromFile(outputImage);
} else {
imageUri = FileProvider.getUriForFile(DialogActivity.this, "tian.project.easytostop.fileprovider", outputImage);
}
// // 启动相机程序
System.out.println("获取照片路径:" + imageUri);
System.out.println("获取照片路径:" + imageUri.getPath());
// Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// startActivity(intent);
// //需要回调时访问事件的方法-registerForActivityResult必须在生命周期STARTED之前调用
// registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
// result -> {
// Intent data=result.getData();
// System.out.println("回调数据"+data);
//
// })
intentActivityResultLauncher.launch(new Intent("android.media.action.IMAGE_CAPTURE").putExtra(MediaStore.EXTRA_OUTPUT, imageUri));
//上传照片
}
});
dialog.findViewById(R.id.tv_take_pic).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("DialogActivity", "onCreate: 获取相册!");
dialog.dismiss();
}
});
dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("DialogActivity", "onCreate:取消操作!");
dialog.dismiss();
Intent intent=new Intent(DialogActivity.this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
/**
* 监听返回事件
*/
// @Override
// protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
// System.out.println("监听返回事件");
// }
}