我的应用程序可以选择从相机拍摄照片。我正在使用手机的默认相机。 问题是,当我拍摄照片时,在我的onActivityResult中,我从intent(数据)中获取null。所以它显示消息图片未被拍摄!“。
调用意图的代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Create a File reference to access to future access
photoFile = getPhotoFileUri(photoFileName);
// wrap File object into a content provider
// required for API >= 24
// See https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
Uri fileProvider = FileProvider.getUriForFile(MainActivity.this, "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileProvider);
// If you call startActivityForResult() using an intent that no app can handle, your app will crash.
// So as long as the result is not null, it's safe to use the intent.
if (intent.resolveActivity(getPackageManager()) != null) {
// Start the image capture intent to take photo
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
});
}
onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
// by this point we have the camera photo on disk
Bitmap takenImage = BitmapFactory.decodeFile(String.valueOf(photoFile));
// RESIZE BITMAP, see section below
// Load the taken image into a preview
img.setImageBitmap(takenImage);
} else { // Result was a failure
Toast.makeText(this, "Picture wasn't taken!", Toast.LENGTH_SHORT).show();
}
}
为照片制作文件:
public File getPhotoFileUri(String fileName) {
// Only continue if the SD Card is mounted
if (isExternalStorageAvailable()) {
// Get safe storage directory for photos
// Use `getExternalFilesDir` on Context to access package-specific directories.
// This way, we don't need to request external read/write runtime permissions.
File mediaStorageDir = new File(
getExternalFilesDir(Environment.DIRECTORY_PICTURES), APP_TAG);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
Log.d(APP_TAG, "failed to create directory");
}
// Return the file target for the photo based on filename
File file = new File(mediaStorageDir.getPath() + File.separator + fileName);
return file;
}
return null;
}
AndroidManifest.xml中的提供者标记
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.example.houman.myapplication/files/Pictures" />
</paths>
"Taking Photos Simply | Android Developers"之后
对我没那么帮助,我试过Accessing the Camera and Stored Media | CodePath
但现在我遇到了这个问题。
答案 0 :(得分:0)
我从意图(数据)
获得null
您没有使用data
,在EXTRA_OUTPUT
方案中,相机应用无需在结果中提供Intent
。所以,通过替换:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK && data != null) {
使用:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {