當活動打開時,會打開一個對話窗口,要求用戶從相機拍攝照片或從相冊中選取一張照片。只要我點擊按鈕打開相機,應用程序崩潰,我得到一個空指針異常與獲取URI相關。我一直在關注Google的演練,以便從相機中保存照片,並且似乎無法找到問題所在。嘗試獲取文件的URI時獲取空指針異常
線發生錯誤:
Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
"xyz.beerme.beerme.provider",
photoFile);
所有方法:
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
} catch (IOException ex){
Snackbar message = Snackbar.make(findViewById(R.id.activity_create_post), "Error creating image", Snackbar.LENGTH_LONG);
message.show();
}
if(photoFile != null){
Uri photoURI = FileProvider.getUriForFile(CreatePostActivity.this,
"xyz.beerme.beerme.provider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
dialog.dismiss();
}
});
createImageFile方法:
private File createImageFile() throws IOException{
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_beerme";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="xyz.beerme.beerme">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:supportsRtl">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="redacted" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="xyz.beerme.beerme.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" ></meta-data>
</provider>
<activity android:name=".PostsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.categroy.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CreatePostActivity"></activity>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/xyz.beerme.beerme/files/Pictures" />
</paths>
您可以將您的方法放在try-catch子句中,並打印異常以查看該URI發生了什麼。 – statosdotcom
這絕對是發生在我認爲,這是erorr'java.lang.NullPointerException:嘗試調用虛擬方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm。 PackageManager,java.lang.String)'空對象引用' – Josh
您的方法試圖運行的活動的名稱爲CreatePostActivity嗎? – statosdotcom