启动相机并为图片创建名称会将 null Intent 返回给 onActivityResult
Launching camera and creating a name for the picture is returning null Intent to onActivityResult
我的问题出在这一行:
pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());
当我评论它时,相机不会 return 向 onActivityResult 发送 null 意图,但是当我取消评论它时,它会向 Intent 数据发送 null,但它会使用我指定的名称保存文件。
代码如下:
启动相机:
private void invokeCamera() {
// get a file reference
Uri pictureUri;
pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// tell the camera where to save the image.
intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
// tell the camera to request WRITE permission.
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
创建文件:
private File createImageFile() {
File imageFile;
// the public picture director
File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String IMGname = Ti.getText().toString() + IMGCounter;
// put together the directory and the timestamp to make a unique image location.
imageFile = new File(picturesDirectory, IMGname + ".jpg");
Toast.makeText(this, IMGname + ".jpg", Toast.LENGTH_LONG).show();
return imageFile;
}
onActivityResult 代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && data != null){
if(requestCode == REQUEST_IMAGE_CAPTURE) {
Toast.makeText(this, "Reached REq", Toast.LENGTH_SHORT).show();
// AddImgToView(data);
}else if(requestCode == IMAGE_GALLERY_REQUEST){
AddImgToView(data);
}
}else{
Toast.makeText(this, "Op 2"+data, Toast.LENGTH_SHORT).show();
}
}
舱单代码:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<!--Required Permissions-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:authorities="company.com.retrofit.provider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InsertData"></activity>
</application>
检查写入外部存储权限,实现动态权限模型,否则我看代码没有错
我发现当您发送缩略图时默认的 Android 摄像头会发送数据,但是当您使用 EXTRA_OUTPUT
时它会发送 null 并且我必须使用我指定的 URI。
我的问题出在这一行:
pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());
当我评论它时,相机不会 return 向 onActivityResult 发送 null 意图,但是当我取消评论它时,它会向 Intent 数据发送 null,但它会使用我指定的名称保存文件。
代码如下:
启动相机:
private void invokeCamera() {
// get a file reference
Uri pictureUri;
pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// tell the camera where to save the image.
intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);
// tell the camera to request WRITE permission.
intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
创建文件:
private File createImageFile() {
File imageFile;
// the public picture director
File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String IMGname = Ti.getText().toString() + IMGCounter;
// put together the directory and the timestamp to make a unique image location.
imageFile = new File(picturesDirectory, IMGname + ".jpg");
Toast.makeText(this, IMGname + ".jpg", Toast.LENGTH_LONG).show();
return imageFile;
}
onActivityResult 代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && data != null){
if(requestCode == REQUEST_IMAGE_CAPTURE) {
Toast.makeText(this, "Reached REq", Toast.LENGTH_SHORT).show();
// AddImgToView(data);
}else if(requestCode == IMAGE_GALLERY_REQUEST){
AddImgToView(data);
}
}else{
Toast.makeText(this, "Op 2"+data, Toast.LENGTH_SHORT).show();
}
}
舱单代码:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<!--Required Permissions-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:authorities="company.com.retrofit.provider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InsertData"></activity>
</application>
检查写入外部存储权限,实现动态权限模型,否则我看代码没有错
我发现当您发送缩略图时默认的 Android 摄像头会发送数据,但是当您使用 EXTRA_OUTPUT
时它会发送 null 并且我必须使用我指定的 URI。