我正在使用意图使用设备的相机。当我使用Uri.fromFile(文件文件)时,应用程序不会停止,图像将保存到目标文件夹。但是当我将Uri.fromFile更改为FileProvider.getUriForFile()时,相机在拍照后停止工作。如何解决此问题以及如何将捕获的图像成功保存到目标文件夹中?
MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initInstances();
}
public void initInstances(){
button = (Button) findViewById(R.id.photoButton);
imageView = (ImageView) findViewById(R.id.imageId);
}
public void takePhoto(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File storageDir = new File(Environment.getExternalStorageDirectory(), "Bry-Test");
Uri.from
storageDir.mkdirs();
File image = new File(storageDir, "QR_" + timeStamp + ".png");
// Uri photoUri = Uri.fromFile(image);
Uri photoUri = FileProvider.getUriForFile(MainActivity.this, "com.test.bry", image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(cameraIntent, 1);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.bry">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="com.test.bry"
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/provider_paths" />
</provider>
</application>
</manifest>
provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>