我需要创建一个目录并在那里保存图像,所以我尝试了很多东西,我在google页面上找到了这个教程:Tutorial
所以,我这样做了:
openCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dispatchTakePictureIntent();
}
});
rivate void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.apps.cadcom.fileprovider",
photoFile);
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, Util.REQUEST_PERMISSION_CAMERA);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
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 */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
在清单中:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.apps.cadcom.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
provider_path.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="my_images" path="Android/data/com.apps.cadcom/files/Pictures" />
</paths>
我没有改变任何内容,这与教程中的代码完全相同。所以我很困惑。调用onActivityResult
方法时,变量data
为空,但resultCode
为-1。
答案 0 :(得分:2)
当onActivityResult方法调用时,变量数据为空
那是因为它应该是null
。除了结果代码之外,不应该通过onActivityResult()
传递任何输出。
你知道这张照片应该在哪里。它应该在photoFile
。这是您通过EXTRA_OUTPUT
要求相机应用程序放置照片的地方。所以,首先在那里寻找它。然后,如果照片不在那里,请查看它是否是一个有缺陷的相机应用程序,并通过Uri
传递给Intent
返回onActivityResult()
。