问题描述
在使用Camera Intent时遇到一个问题,在OnActivityResult中,我将currentPhotoPath
设置为null。在三星M20(版本10)和一加(版本10)中试用。实现了Google doc的代码形式。相同的代码在Google pixel3(版本11),Redmi note 7s(版本9),Vivo(版本8.1)中都能正常工作。
https://developer.android.com/training/camera/photobasics
public class MainActivity extends AppCompatActivity {
Button button;
String currentPhotoPath;
TextView textView;
File photoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
textView = findViewById(R.id.text);
button.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
}
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.v("currentPhotoPath","currentPhotoPath onStart " + currentPhotoPath);
}
@Override
protected void onResume() {
super.onResume();
Log.v("currentPhotoPath","currentPhotoPath onResume " + currentPhotoPath);
}
@Override
protected void onRestart() {
super.onRestart();
Log.v("currentPhotoPath","currentPhotoPath onRestart " + currentPhotoPath);
}
@Override
protected void onPause() {
super.onPause();
Log.v("currentPhotoPath","currentPhotoPath onPause " + currentPhotoPath);
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printstacktrace();
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,"com.example.camera.fileprovider",photoFile);
Log.v("currentPhotoPath","currentPhotoPath file photoFile " + photoFile.getAbsolutePath());
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent,2);
}
}
}
@Override
protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
Log.v("currentPhotoPath","currentPhotoPath onActivityResult " + currentPhotoPath);
if (currentPhotoPath != null) {
textView.setText(currentPhotoPath);
} else {
Toast.makeText(this,"Something went wrong,Unable to pick PDF file",Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode,permissions,grantResults);
if (requestCode == 1) {
dispatchTakePictureIntent();
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,/* prefix */
".jpg",/* suffix */
storageDir /* directory */
);
Log.v("currentPhotoPath","currentPhotoPath file path " + image.getAbsolutePath());
currentPhotoPath = image.getAbsolutePath();
Log.v("currentPhotoPath","currentPhotoPath file " + currentPhotoPath);
return image;
}
}
解决方法
第1课:当您的活动不是最重要的活动时,Android OS随时可能终止您的活动。
当相机应用程序排名第一时,您的活动显然被杀死。
将变量保存在onSaveInstanceState()中,然后在其参数不为null时将其加载回onCreate()中。