【发布时间】:2020-08-31 07:28:51
【问题描述】:
我在使用相机意图时遇到一个问题,在 OnActivityResult 中我将currentPhotoPath 设为空。在三星 M20(第 10 版)和一加(第 10 版)中尝试 实现了 Google 文档的代码。相同的代码在 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;
}
}
【问题讨论】:
-
您好,问题解决了吗?
标签: android camera android-camera-intent