我已经看过许多stackoverflow的问题,但不幸的是,没有一个能满足我的需求。
我得到了这个例外:
有罪的阶层是:
public class BitmapSaving {
private Context context;
public static File mediaFile;
private static String mCurrentPhotoPath;
private static Thread threadStoreImage, threadCreateFile;
public BitmapSaving(Context context){
this.context = context;
}
private Boolean isSDPresent =
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
/**
* Stores the image after taking a picture
*/
public void storeImage() {
if(isSDPresent && isExternalStorageWritable()){// Check if SDCard (or internal storage for New Devices) is present
threadStoreImage = new Thread(new Runnable() {
@Override
public void run() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
});
threadStoreImage.run();
Log.d(this.getClass().getSimpleName(),"threadStoreImage.run();");
}
}
private File image = null;
/** Create a File for saving an image or video
* @return
*/
public File createImageFile(){
// Create an image file name
threadCreateFile = new Thread(new Runnable() {
@Override
public void run() {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStorageDirectory(), "App_Name");
try {
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
}
});
threadCreateFile.run();
Log.d(this.getClass().getSimpleName(),"threadCreateFile.run();");
return image;
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
}

我称之为BitmapSaving的类的片段:
case R.id.takeAPictureBtn:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
// Continue only if the File was successfully created
File photoFile = null;
photoFile = new BitmapSaving(getActivity()).createImageFile();
if (photoFile != null) {
uri = FileProvider.getUriForFile(getActivity(),
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(takePictureIntent, CAMERA);
}
}
break;

在清单中,我设置了READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE权限。
我也在Manifest中设置了这个:
<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>
&#13;
XML文件路径是:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="App_Name"/>
</paths>
&#13;
在我的真实设备(Android 6.0)上一切正常。
当涉及到模拟器时,它会在每个Android版本(从22到25)中与此异常崩溃,并且不会触发相机意图。
提前谢谢。