【发布时间】:2018-04-01 01:27:12
【问题描述】:
我的问题出在这一行:
pictureUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", createImageFile());
当我评论它时,相机不会向 onActivityResult 返回 null 意图,但是当我取消评论它时,它会将 null 发送到 Intent 数据,但它会以我指定的名称保存文件。
代码如下:
启动相机:
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>
【问题讨论】:
-
我确定它是否会对您有所帮助,但是您是否尝试过为您的应用程序添加写入存储权限?
-
@AntonMakov 再次检查代码我添加了 Manifest,我的问题在于 pictureUri 部分。当我删除它时,代码工作,当我添加它时,它一直发送空数据。
标签: android android-intent android-camera uri onactivityresult